I dont need to write this in any language just algorithms Qu
I don\'t need to write this in any language, just algorithms.
Quick Sort:
Array={39,-2,45,86,0,10}
select the last element (10) as the pivot.
1. Find the index (position for 10) elements which are smaller than 10 are in the left, greater than 10 are in the right. Therefore, you can find the position for 10
a) Start from the 1st element of the array
39>10, swap the pivot (10) with 39
{10 -2,45,86,0, ||39}
b) 10 is the pivot, since 10 is at the beginning of the array, we start from the end of the unsorted part. Compare 10 with 0, 10>0, swap them.
{0 ||-2,45,86,10, ||39}
c) 10 is the pivot, since 10 is at the end of the array, we start from the beginning of the unsorted part. Compare -2 with 10, -2<10, no need to swap.
{0 -2||,45,86,10, ||39}
d) 10 is the pivot, compare 45 with 10, and swap them.
{0, -2 || 10, 86||45,39}
e) 10 is the pivot, compare 86 with 10, no need to swap.
{0, -2 || 10||86,45,39}
f) Find the position of 10. Array[2]=10
2. Array has been slipped into two parts. The left parts are element smaller than 10, the right part is the elements which are greater than 10.
a) use quick sort to sort the left part.
{0,-2}
i) -2 is the pivot, compare 0 and -2, swap them,we find the index (position) for -2 Array[0]=-2
ii) we find the index for 0, too. Array[1]=0
so the array is: {-2,0,10||86,45,39}
b) Using quick sort to sort the right part
{86,45,39}
We select 39 as the pivot. We start from the beginning of the unsorted part compare 86 with 39, swap them
{39,45,||86}
Compare the pivot (39) with 45, do not need to swap.
{39,||45,86}
We find the index (position) for 39 Array[3]=39
The array is {-2,0,10,39||45,86}
3. The unsorted part is {45,86}
We select 86 as the pivot, compare 45 with 86, do not need to swap them. The index of 45, and 86 are found.
The sorted array is: {-2,0,10,39, 45,86}
Merge Sort:
Array={39,-2,45,86,0,10}
Split the array into two parts
A={39,-2,45} || B={86,0,10}
Split A and B into four parts
A1={39}, A2={-2,45},|| B1={86},B2={0,10}
3. Split A1,A2,B1,B2 into eight parts
A1={39}, A2a={-2}, A2b={45}, || B1={86},B2a={0},B2b={10}
4.Merge:
Compare A2a and A2b (-2<45), B2a and B2b (0<10)
A1={39}, A2={-2,45}, B1={86},B2={0,10}
5.Compare A1 with the smaller elements of A2, compare B1 with the smaller element of B2, swap if necessary
39<-2 swap; compare 39 with 45 no need to swap A= {-2,39,45}
86>0, swap; compare 86 with 10, swap. B={0,10,86}
6.Merge A and B
Compare -2 with 0 no need to swap Array={-2,0
Compare 39 with 10, swap them Array={-2,0,10,39
Compare 45 with 86, no need to swap Array={-2,0,10,39,45,86}
Solution
1) Quick Sort
Basically in quick sort divide and conquer strategy is used, We chose one element called as pivot value and the parition the array and then sort both parts and continue doing this until complete array is sorted.
a) Choose a pivot element.
b) Take two values to point to left and right to array excluding pivot where left points to a low index and high point right to high index.
c) while value at left is less than value at pivot keep moving right.
d) while value at rigth is greater than pivot keep moving left.
e) if step c & d doesn\'t match swap left and right.
f) When left is greater than right, the point where the met is new pivot.
Merge Sort : it also works on divide and conquer , but it divides the array in two halves and then combine them in sorted manner.
a) First check if there is only one element in array then return true.
b) Divide the array recursively in two halves until it cann\'t be further divided.
c) And then merger smaller list into new list in sorted manner.


