• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me learn sorting  RSS feed

 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void insert(int[]x,int i)
{

int temp=x[i]
intj=i-1;

while(j>=0 && temp<x[j])
{
x[j+1]=x[j];
J--;
}
x[j+1]=temp;
}


Its asking me to sort


2 3 5 9 4

I'm having trouble learning this sorting method. I understand that the first part creates a temp to store the value 4, then the next part creates j to compare the last cell with the second to last cell. The while loop simply makes sure that it doesn't go past the first cell and is comparing the values of the temporary value with that of j(the second to last).

the whole shifting right confuses me
namely the x[j+1]=x[j];
j--

from what i can gather it is taking j's value which is 9 and adding 1 to it but that doesn't make sense to me. I do understand that the j-- is simply subtracting 1 to set up the next comparison in the array

if i was wrong in any part of my summary please tell me as i am eagerly trying to learn this
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many ways to sort an array or list using the Collections framework. These API can do a whole bunch of stuff.

From your method, is there any restrictions (eg can't use Collection framework or particular methods)?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you calling that method insert?
That looks like code where you are guessing. Assuming you are supposed to write a sort method, I suggest you go back to pencil and paper and write down the algorithm. A lot of sotring methods swap two elements in an array, so maybe a good start is writing a swap method. I shall give you a startGet that working, then look up sorting algorithms. Bubble sort is the least efficient, but you will probably want that because it is the easiest to write and understand.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Bubble sort is the least efficient, but you will probably want that because it is the easiest to write and understand.

Oh, no. Bubble sort is not the least efficient. The least efficient is Bogosort .
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote: . . . The least efficient is Bogosort .
 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this is the code straight out of textbook so i'm not guessing at all, can anyone answer my question tough since this is the code my professor wants us to learn
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Davey Lopez wrote:from what i can gather it is taking j's value which is 9 and adding 1 to it but that doesn't make sense to me.

If you have j equal to 9 in this program, you are in trouble (because your array is of length 5). The variable j holds an index, not the value.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to learn about sorting, and your instructional materials are not sufficient, Wirth's book Algorithms and Data Structures has a chapter on that. You'll find a link to its free PDF version in http://www.coderanch.com/how-to/java/HereYouWillFindLinksToFreeStuff
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that part of a quicksort? Please always tell us where such code is from, to avoid potential copyright problems.
 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Davey Lopez wrote:from what i can gather it is taking j's value which is 9 and adding 1 to it but that doesn't make sense to me.

If you have j equal to 9 in this program, you are in trouble (because your array is of length 5). The variable j holds an index, not the value.



It sorting a hand of playing cards 2, 3, 5, 9, 4.
currently j should be set the the second to last cell as noted by int j=i-1

It's from Java programming from the ground up by ralph braco and shai simonson

can anyone just tell me if I got my assumption about x[j+1]=x[j]; correct?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Davey Lopez wrote:can anyone just tell me if I got my assumption about x[j+1]=x[j]; correct?

Davey Lopez wrote:from what i can gather it is taking j's value which is 9 and adding 1 to it but that doesn't make sense to me. I do understand that the j-- is simply subtracting 1 to set up the next comparison in the array

No, that is not exactly correct. j is an INDEX into an array. It is keeping track of where you are in the array. So j's value is not 9. j's value is 3. so x[j] means the value at j'th position in the array - that is your 9.

so x[J+1] is the "j+1th" element in the array, or position 4. so we put the value at position three (9) into the slot at position 4, overwriting the value of 4.
 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Davey Lopez wrote:can anyone just tell me if I got my assumption about x[j+1]=x[j]; correct?

Davey Lopez wrote:from what i can gather it is taking j's value which is 9 and adding 1 to it but that doesn't make sense to me. I do understand that the j-- is simply subtracting 1 to set up the next comparison in the array

No, that is not exactly correct. j is an INDEX into an array. It is keeping track of where you are in the array. So j's value is not 9. j's value is 3. so x[j] means the value at j'th position in the array - that is your 9.

so x[J+1] is the "j+1th" element in the array, or position 4. so we put the value at position three (9) into the slot at position 4, overwriting the value of 4.


Thank you so much that cleared up everything, I'll post my summary later just to make sure
-thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!