Ads

Ads

View Answers

July 3, 2009 at 11:04 AM

Hi Friend,

In Bubble Sort, array is traversed from 0 to the length-1 index of the array and compared first two values and put the larger one at higher index. Then take next two values compare these values and place larger value at higher index. This process do iteratively until the largest value is not reached at last index. Then start again from zero index up to n-1 index. The algorithm follows the same steps iteratively unlit elements are not sorted.

For ex,if we have an array unsorted A[0],A[1],A[2]................ A[n-1] and A[n] as input. Then the following steps are followed by bubble sort algorithm to sort the values of an array.

1.Compare A[0] and A[1] .

2.If A[0]>A[1] then Swap A[0] and A[1].

3.Take next A[1] and A[2].

4.Comapre these values.

5.If A[1]>A[2] then swap A[1] and A[2]

................................

at last compare A[n-1] and A[n]. If A[n-1]>A[n] then swap A[n-1] and A[n]. As we see the highest value is reached at nth position. At next iteration leave nth value. Then apply the same steps repeatedly on A[0],A[1],A[2]................ A[n-1] elements repeatedly until the values of array is sorted.

While In quick sort algorithm pick an element from array of elements. This element is called the pivot. Then compare the values from left to right until a greater element is find then swap the values. Again start comparison from right with pivot. When lesser element is find then swap the values. Follow the same steps until all elements which are less than the pivot come before the pivot and all elements greater than the pivot come after it. After this partitioning, the pivot is in its last position. Recursively sort the sub-array of lesser elements and the sub-array of greater elements.

For more information,visit the following links:

http://roseindia.net/java/beginners/arrayexamples/QuickSort.shtml

http://roseindia.net/java/beginners/arrayexamples/bubbleSort.shtml

Thanks

In Bubble Sort, array is traversed from 0 to the length-1 index of the array and compared first two values and put the larger one at higher index. Then take next two values compare these values and place larger value at higher index. This process do iteratively until the largest value is not reached at last index. Then start again from zero index up to n-1 index. The algorithm follows the same steps iteratively unlit elements are not sorted.

For ex,if we have an array unsorted A[0],A[1],A[2]................ A[n-1] and A[n] as input. Then the following steps are followed by bubble sort algorithm to sort the values of an array.

1.Compare A[0] and A[1] .

2.If A[0]>A[1] then Swap A[0] and A[1].

3.Take next A[1] and A[2].

4.Comapre these values.

5.If A[1]>A[2] then swap A[1] and A[2]

................................

at last compare A[n-1] and A[n]. If A[n-1]>A[n] then swap A[n-1] and A[n]. As we see the highest value is reached at nth position. At next iteration leave nth value. Then apply the same steps repeatedly on A[0],A[1],A[2]................ A[n-1] elements repeatedly until the values of array is sorted.

While In quick sort algorithm pick an element from array of elements. This element is called the pivot. Then compare the values from left to right until a greater element is find then swap the values. Again start comparison from right with pivot. When lesser element is find then swap the values. Follow the same steps until all elements which are less than the pivot come before the pivot and all elements greater than the pivot come after it. After this partitioning, the pivot is in its last position. Recursively sort the sub-array of lesser elements and the sub-array of greater elements.

For more information,visit the following links:

http://roseindia.net/java/beginners/arrayexamples/QuickSort.shtml

http://roseindia.net/java/beginners/arrayexamples/bubbleSort.shtml

Thanks

Ads

Related Tutorials/Questions & Answers: