Sorting

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Algorithm Straight Insertion Sort

1. B[1]=A[1] // initially the first element in the INPUT list and the
OUTPUT list
2.For J=2 to N do // Continue taking one key at a time from the
input list
/*Select the Key K from the input List */
3. K=A[ j ] // K is the key under insertion
/* select the location ‘i’ in the output list such that Ki <= K< ki+1 */
4. flag= TRUE // To control the number of scan in the out put list
5.i=j-1 //points the right most element in the out put list
6.While(flag=TRUE) do
7. if(K<B[i]) then
8. i=i-1
9. if(i=0) then
10. flag=FALSE
11. EndIf
12. Else
13. flag=FALSE
14. EndIf
15.EndWhile
/* Move to the right on place all the Keys from and
After i+1 */
16.p=j
17. While(p>i+1)do
18.B[p]=B[p-1]
19.p=p-1
20.EndWhile
/*Insert the Keys at (i+1)th place */
21.B[i+1]=K // insert the key at the (i+1) th place
22. EndFor
23.Stop
Algorithm List Insertion Sort
1. HEADER DATA=NULL, HEADERLINK=NULL
2. New=GetNode(), newDATA =A[1],
newLINK=NULL
3. HEADERLINK=new
4. For j=2 to N do
5. K=A[ j ]
6. new=GetNode(), newDATA=K, newLINK=NULL
/* Search the location in the out put list such that
Ki <= K < Ki+1 */
7. ptr1= HEADER, ptr2= HEADER LINK
flag = TRUE
8.While (Flag=True) and (Ptr2!=Null)do
9. If(ptr2DATA<=K)then
10.ptr2=ptr2Link, ptr1=ptr1Link
11.Else
12.Flag=FALSE
13.EndIf
14.EndWhile
15.ptr1Link=new, newLink=ptr2
16.EndIf
17.Stop
Algorithm BinaryInsertionSort
1. B[1]=A[1] //Initially the first element is inserted without any
comparison
2. For J=2 to N do // Continue taking one key at a time from the
input list A
3. K=A[j] //The next element to be inserted
/*Search the list to find the location for the Key K */
4. L=1; R=j-1 //Two Pointers to the two ends of the output list B
5.loc=BinarySearch(K,L,R) //Call the probe to find the place of
insertion of K
/* Move the keys starting from the location “loc” */
6. i=j-1
7. While(I > loc) do
8. B[i+1]=B[i]
9. i=i-1
10. EndWhile
11. B[i+1]=K //Insert the key at “loc”
12. EndFor
13. Stop
Algorithm BinarySearch
1. If(R<=L) then //Termination Condition
2. If (B[L]>K) then
3. loc=L-1
4. Return(loc)
5. Else
6. loc=L
7. Return(loc)
8. EndIf
9. EnfIf
10. mid=(L+R)/2 //Return the lowest integer on
division
11. If(K<B[mid]) then
12. R=mid-1
13. loc=BinarySearch(K,L,R) //Recursive search at
left half
14. Return(loc)
15. EndIf
16. If(K>B[mid]) then
17. L=mid+1
18. loc=BinarySearch(K,L,R) //Recursive search at
right half
19. Return(loc)
20. EndIf
21. If(K = B[mid] then
22. loc=mid //Element is found here
23. Return(loc)
24. Stop
Algorithm StraightSelectionSort
1. For i=1 to (n-1) do //(n-1) Iterations
2. j=SelectMin(i,n) //Select the Smallest from the
remaining part of the list
3.If(i != j) //Interchange when the minimum is in
remote
4. Swap(A[i],A[j])
5. EndIf
6. EndFor
7. Stop
Algorithm SelectMin
1. min=A[L] // Initially the item at the starting
location is chosen as the smallest
2.minLoc = L
3.For i = L+1 to R do
4.If (min > A[i]) then
5.Min = A[i]
6. minLoc = i
7.EndIf
8.EndFor
9. Return(minloc)
10. Stop ex: 4 6 7 5 9 3 1 8 2
Algorithm CreateHeap
1. i=1 // Initially , the HEAP Tree B is empty and
starts with the first element in A
2. While(I <= n)do // Repeat for all elements in the
Array A
3. x=A[i] // select i th element from the list A
4. B[i]=x // Add the element at the i th place in the
array B
5. j=1 // j is the current location of the element in B
6. While (j > 1) do //Continue until the Root is checked
7. If B[j] > B[j/2] then //its violates the heap(max)
property
8. temp = B[j] //Swap the elements
9. B[j] = B[j/2]
10. B[j/2] = temp
11. j = j/2 // Go to the parent node
12. Else
13. j = 1 //Satisfies the heap property , terminate
these inner loop
14. EndIf
15. EndWhile
16. i = i+1 // select the next element from the
input list
17. EndWhile
18. Stop
Algorithm RebuiltHeap
1. If (i=1) then
2. Exit // No rebuild with the single element in the list
3. j=1 //ELSE start with the root node
4. flag = TRUE // Rebuild is required
5. While (flag = TRUE) do
6. leftChild = 2*j , rightChild = 2*j+1
7. if(rightChild <= i) then
/*Compare whether the left child or the right child will
move to up or not*/
8. if(B[j] <= B[leftChild]) AND
B[leftChild >= B[rightChild] then //Parent and Left child
violate the heap property
9. Swap (B[j], B[leftChild]) // Swap the parent and the left
child
10. j = leftChild //Move down to nade at the net
level
11. Else
12. If (B[j] <= B[rightChild]) AND
B[rightChild >= B[leftChild] then //Parent and the
right child violate the heap Property
13. Swap( B[j],B[rightChild]) //Swap the Parent and
Right Child
14. j = rightChild //Move down to node at the next level
15. Else //Heap Property is not Violated
16. Flag = FALSE
17.EndIf
18. EndIf
19. Else
20. If (leftChild <= i ) then
21. If ( B[j] <= B[leftChild] ) then //Parent and left
child violate the heap property
22. Swap (B[j] , B[leftChild] ) //Swap the parent and
the left child
23. j= leftChild //Move down to node at the next
level
24. Else //Heap Property is not violated
25. Flag = FALSE
26. EndIf
27. EndIf
28. EndIf
29. EndWhile
30. Stop
Algorithm HEAPSORT
1. Read n elements and store in the array A
2. CreatHeap (A)
3. For i=n down to 2 do //Repeat n-1 times
4. RemoveMax(B,i) //Remove the element at the
Root and swap it with the i th
5. RebuiltHeap(B, i-1) //Rebuilt heap with the
elements B[1,2, …, (i-1)], i>1
6. EndFor
7. Stop
Ex: 15 35 55 75 05 95 85 65 45 25
Algorithm BUBBLESORT
1. For i = 1 to n-1 do
2. For j = 1 to n-i do
3. if (A[j] > A[j+1]) then
4. Swap( A[j] , A[j+1])
5. EndIf
6. EndFor
7. EndFor
8. Stop
Ex: 55 77 99 33 22 88 66 44
Algorithm ShellSort
1. l = (log2(2n/3+1)/log23-1) // hl denotes the largest increments
for a given value of n
2. hl=(3l-1)/2 // Calculates the value of the largest increment hl
3. hi= hl // hi denotes the value of the increment in the i th
pass
4. While (hi > 0) do // Continue until the lowest increment is
done
5. j = hi // Index to an element in a subsequence in the current
pass
6. While (j<=n)do // This loop takes care of all subsequences in
the pass with hi
7. j = j+1
8. k = j- hi
9. While (k>0)do // This loop implements the insertion sort
10. If A[k] > A[k + hi] then
11. Swap(A[k] , A[k + hi] )
12. k = k- hi
13. Else
14. k = 0 // Reset of the current subsequence is in
order
15. EndIf
16. EndWhile
17. EndWhile
18. hi = (hi -1)/3 // Calculates the increment value
for the next pass
19. EndWhile
20. Stop Ex: 21 72 43 94 85 16 67 38 59 91 32 73
24 45 56 60
Algorithm QuickSort
1. left= low , right = high // left and right are two
pointers to locate partitions
2. IF (left < right) then // check the termination
condition
3. Loc = partition(A, left ,right)
4. QuickSort(A,left,loc-1) // perform quick sort over
the left sub-list
5. QuickSort(A,loc+1,right)// perform quick sort
over the right sub-list
6. Endif
7. Stop Ex : 55 88 22 99 44 11 66 77 33

You might also like