Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Help understanding insertion sort algorithms for certain methods in code  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

// this is the main code our teacher gave us to mess with to try to figure out how to sort social security number with a tmp variable


//Im totally lost on how to sort it with the code he gave us I dont understand how to write my for loops to make them be sorted in ascending order thats all  have for now but i feel like its totally wrong i thought i would need to make a current but he told me i didnt have to i just need help understanding where to go i also have the rest of the code & other methods too.
 
Bartender
Posts: 10750
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the main problem you're having is that you're coding far too soon.
A couple of questions:
1. Do you understand how Insertion Sort works?
2. Can you describe it in English (or your native language)?
Because until you can, trying to code it in Java is just a waste of time.

And right now it looks like you're getting tied up in knots with bits and bytes and DbObjects and RandomAccessFiles.
One link you might find useful: StopCoding.

Winston
 
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

As well as what Winston has already told you:
Have you copied that code correctly? Or is it what you ended up with after messing about with it? Lines 11 and 13 won't compile together. I think you have duplicated something.
I don't think that code is very good. The block and the method are far too long. There also seems to have code in there which doesn't belong in an insertion sort method. It is mixing reading from the file, reading from the console, and sorting in that method; those things should be done in separate methods.
It is also opening the same file twice (lines 2 and 9) and not closing it correctly. It is not correct, despite what you see in many books, simply to call myFile.close();. You should use try with resources, which makes absolutely sure to close the file.
 
Winston Gutkowski
Bartender
Posts: 10750
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another suggestion: Simplify the problem.
Your teacher has added a lot of extraneous stuff like random access files and temp variables that don't actually have anything to do how to DO an Insertion Sort.
I think I know why they did it, but it really confuses the issue.
So...
Can you write an Insertion Sort that sorts an Array of ints? eg:
If you can, and it works, THEN tackle the business of substituting a RandomAccessFile for the array.

Hope it Helps

Winston
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:. . . THEN tackle the business of substituting a RandomAccessFile for the array. . . .

Or copy the entire file into an array, sort the array, and write the results back. That has the advantage that you can make the sorting independent of the type of file.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!