Davey Lopez

Ranch Hand

Posts: 33

posted 3 years ago

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

{

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

Campbell Ritchie

Marshal

Posts: 56599

172

posted 3 years ago

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.

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.

posted 3 years ago

Oh, no. Bubble sort is not the least efficient. The least efficient is Bogosort .

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

Davey Lopez

Ranch Hand

Posts: 33

Ulf Dittmer

Rancher

Posts: 42972

73

posted 3 years ago

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

posted 3 years ago

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

posted 3 years ago

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?

Pawel Pawlowicz wrote:Davey Lopez wrote:from what i can gather it is takingj's value which is 9and adding 1 to it but that doesn't make sense to me.

If you havejequal to 9 in this program, you are in trouble (because your array is of length 5). The variablejholds 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?

posted 3 years ago

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 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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Davey Lopez

Ranch Hand

Posts: 33

posted 3 years ago

Thank you so much that cleared up everything, I'll post my summary later just to make sure

-thanks again

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