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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Jeanne Boyarsky
• Tim Cooke
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Mikalai Zaikin
• Carey Brown
Bartenders:

# Help me learn sorting

Ranch Hand
Posts: 33
• Number of slices to send:
Optional 'thank-you' note:
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;
}

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

Bartender
Posts: 3648
16
• Number of slices to send:
Optional 'thank-you' note:
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)?

Marshal
Posts: 78675
374
• Number of slices to send:
Optional 'thank-you' note:
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.

Bartender
Posts: 2236
63
• Number of slices to send:
Optional 'thank-you' note:

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: 78675
374
• Number of slices to send:
Optional 'thank-you' note:

Pawel Pawlowicz wrote: . . . The least efficient is Bogosort .

Davey Lopez
Ranch Hand
Posts: 33
• Number of slices to send:
Optional 'thank-you' note:
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: 2236
63
• Number of slices to send:
Optional 'thank-you' note:

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.

Rancher
Posts: 43081
77
• Number of slices to send:
Optional 'thank-you' note:
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 https://coderanch.com/how-to/java/HereYouWillFindLinksToFreeStuff

Campbell Ritchie
Marshal
Posts: 78675
374
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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?

lowercase baba
Posts: 13089
67
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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

 The knights of nee want a shrubbery. And a tiny ad: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com