can some one please explain me this ?? step by step ?
tutorial 41  sorting arrays 
Post a Reply 
Replies 
Oldest Newest Rating 
+1 
Dol Lod
· February 23, 2015
He is just covering how to do basic supporting. I can try to explain it a bit better, though I think Bucky covers it pretty well. The first sort he does is bubble sort.
Bubble sort is a very simple sort that does one comparison at a time between each number in a round robin fashion. Since you asked for step by step, I will translate what he did into pseudocode. int array[5]={3,4,2,1,5} Step 1: Set the starting position to be 0. Step 2: Set the current position to the starting position+1 . While the current position in the array is less than the size of the array, do the following. Compare the value at the starting position to the value at the current position. If the value at the current position is less than the value at the starting position, swap the values at the starting position and current position. Then increment current position by 1. Step 3: Increment the starting position by 1. Step 4: If starting position is size1, you are done. Else go to step 2. That covers the pseudocode. Now to show what happens every single iteration in this example. int array[5]={3,4,2,1,5} 1st iteration: Starting position=0. Current position=1. We don't know if any elements are sorted prior to this point. 1st comparison is between 3 and 4. 3 is less than 4 so current position increments. 3 is now compared to 2. 2 is less than 3 so a swap is made. The array now looks like {2,4,3,1,5}. 2 is now compared to 1 and 1 is less than 2 so a swap is made. Now the array looks like {1,4,3,2,5} Starting position now increments. 2nd iteration: Starting position=1. Current position=2. We know the first element is the smallest after doing the first iteration. 1st comparison is between 4 and 3. 3 is smaller so a swap is performed. Now the array looks like {1,3,4,2,5}. Now a comparison is done between 3 and 2 and 2 is smaller so a swap is performed. Now the array looks like {1,2,4,3,5}. Now 2 is compared to 5 and 2 is smaller so nothing is done. Staring position increments now. 3rd iteration: Starting position=2. Current position=3. We know the first 2 elements are sorted due to the previous iterations. 1st comparison is between 4 and 3. 3 is smaller so a swap is performed. Now the array looks like this {1,2,3,4,5}. 3 is now compared to 5 and 3 is smaller so nothing is done. Staring position now increments. 4th iteration: Starting position=3. Current position=4. We know the first 3 elements are sorted due to previous iterations. 4 is compared to 5. 4 is smaller so no swap is performed. Now current position is 5. Starting position now increments. The loop is now broken because starting position is now set to 4. out of leaving you with an array of {1,2,3,4,5}. There are many different kinds of sorts. Bubble sort is the easiest to implement, because it just does round robin comparisons but is inefficient. However, it does convey the simplest idea behind sorting. 
0 
imran LP
· March 5, 2015
Arjun Patel Thanks a ton dude that really helped ..perfect.

0 
Dol Lod
· March 5, 2015
Np.

0 
J show
· February 24, 2015
Good explanation.

 1