Case Study On Sorting Algorithm
Case Study On Sorting Algorithm
Roll No:Cs190175
CASE STUDY(Group-6)
1. Q. Write different 20-30 sorting algorithms available and write
their time and space complexity and give comparison view(G-
6).
1.Selection sort
Step 1: Repeat Steps 2 and 3 for K = 1 to N-1
Step 2: CALL SMALLEST (ARR, K, N, POS)
Step 3: SWAP A[K] with ARR[POS]
[END OF LOOP]
Step 4: EXIT
2.HEAP SORT
HEAP_SORT (ARR, N)
Step 1: [Build Heap H]
Repeat for i=0 to N-1
CALL INSERT_HEAP (ARR, N, ARR[i])
[END OF LOOP]
Step 2: Repeatedly Delete the root element
Repeat while N > 0
CALL Delete Heap (ARR, N, VAL)
SET N = N+1
[END OF LOOP]
Step 3: END
3.COUNTING SORT
STEP 1 START
STEP 2 Store the input array
STEP 3 Count the key values by number of occurrences of object
STEP 4 Update the array by adding previous key elements and assigning to
objects
STEP 5 Sort by replacing the object into new array and key= key-1
STEP 6 STOP
4.QUICK SORT
Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG =
Step 2: Repeat Steps 3 to 6 while FLAG =
Step 3: Repeat while ARR[LOC] <=ARR[RIGHT]
AND LOC!= RIGHT
SET RIGHT = RIGHT - 1
[END OF LOOP]
Step 4: IF LOC = RIGHT
SET FLAG = 1
ELSE IF ARR[LOC] > ARR[RIGHT]
SWAP ARR[LOC] with ARR[RIGHT]
SET LOC = RIGHT
[END OF IF]
Step 5: IF FLAG = 0
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT + 1
[END OF LOOP]
Step 6:IF LOC = LEFT
SET FLAG = 1
ELSE IF ARR[LOC] < ARR[LEFT]
SWAP ARR[LOC] with ARR[LEFT]
SET LOC = LEFT
[END OF IF]
[END OF IF]
Step 7: [END OF LOOP]
Step 8: END
Space O(1)
Worst case running time O(n2)
Average case running time O(n)
Best case running time O(n2)
6.MERGE SORT
ADVERTISEMENT
7.CYCLE SORT
cycleSort(array, size)
Begin
for start:= 0 to n – 2 do
key := array[start]
location := start
for i := start + 1 to n-1 do
if array[i] < key then
location:=location +1
done
8.INSERTION SORT
9.RADIX SORT
1. for i=1 to s
2. U[i] =0
5. q=1
6. for j =1 to s
7. while U[ j ]>0
8. T[q]=j
9. U[ j ]=U[ j ]-1
10.q=q+1
Pigeonhole
11.TREE SORT
insert (Node node, Key value):Inserts a new node in the BST
Data: node: The input node of the object
value: The value of node key
Result: Returns the inserted node object
if node!=null then
return Node(value);
end
else if value<=node.value then
node.left<-insert(node.left,value);
end
else
node.right<-insert(node.right,value);
end
return node;
12.SHEEL SORT
Shell Sort(Arr, n)
13.COMB SORT
STEP 1 START
STEP 2 Calculate the gap value if gap value==1 goto step 5 else goto step 3
STEP 3 Iterate over data set and compare each item with gap item then goto
step 4.
STEP 4 Swap the element if require else goto step 2
STEP 5 Print the sorted array.
STEP 6 STOP
Algorithm Complexity
Worst Case Complexity O(n2)
Best Case Complexity θ(n log n)
Average Case Complexity Ω(n2/2p) where p is number of
increments.
Worst Case Space Complexity O(1)
14.BITONIC SORT
Step 1 − Create a bitonic sequence.
Step 2 − Now, we have a bitonic sequence with one part in increasing order and
others in decreasing order.
Step 3 − We will compare and swap the first elements of both halves. Then second,
third, fourth elements for them.
Step 4 − We will compare and swap, every second element of the sequence.
Step 5 − At last, we will compare and swap adjacent elements of the sequence.
Step 6 − After all swaps, we will get the sorted array.
TIME AND SPACE COPLEXITY
In order to form a sorted sequence of length n from two sorted sequences of length n/2, there are
log(n) comparator stages required (e.g. the 3 = log(8) comparator stages to form sequence i from d
and d'). The number of comparator stages T(n) of the entire sorting network is given by:
15.COCKTAIL SORT
2. The first stage loop through the array like bubble sort from left to right. The
adjacent elements are compared and if left element is greater than the right
element, then we swap those elements. The largest element of the list is
placed at the end of the array in the forward pass.
3. The second stage loop through the array from the right most unsorted
element to the left. The adjacent elements are compared and if right element
is smaller than the left element then, we swap those elements. The smallest
element of the list is placed at the beginning of the array in backward pass.
16.TIM SORT
Consider an array of n elements which needs to be sorted. In Tim sort, the array is
divided into several parts where each of them is called a Run. These Runs are sorted
by using insertion sort one by one and then the sorted runs get combined using a
combine function. The idea of Tim sort is originated from the fact that, insertion sort
works more optimally on the short lists rather than working on the larger lists.
18.PANCAKE SORTING
LET GIVEN ARRAY BE arr[] and size be n.
Start from current size equal to n and reduce current size by one while
it’s greater than 1. Let the current size be curr_size. Do following for
every curr_size
o Find index of the maximum element in arr[0..curr_szie-1]. Let the
index be ‘mi’
o Call flip(arr, mi)
o Call flip(arr, curr_size-1)
19.BOGO SORTING
while not Sorted(list) do
shuffle (list)done
TO GENERATE PERMUTATION
1.FOR I<n
2.swamp(a[I])
3. while not Sorted(list) do
shuffle (list)done
19.GNOME SORTING
Gnome Sort also called Stupid sort is based on the concept
of a Garden Gnome sorting his flower pots
1.If you are at the start of the array then go to the
right element (from arr[0] to arr[1]).
2.If the current array element is larger or equal to the
previous array element then go one step right
Time: - O (n log(n))
Space: - O(n)
22.TAG SORTING
Time: -O (n log n)
Space:-O(n)
23.BRICK SORTING OR ODD/EVEN SORTING
function oddEvenSort(list)
while (! sorted) {
sorted = true;
sorted = false; } }
sorted = false; }
WORST-CASE=O(n^2)
BEST-CASE=O(n)
SPACE COMPLEXITY=O (1)
24.RECURSIVE INSERTION SORTING
Step1 − loop from 1 to n-1 and do −
Step2.1 − select element at position i, array[i].
Step2.2 − insert the element in its position in the sorted sub-array array [0] to arr[i].
TIME COMPLEXITY AND SPACE COMPLEXITY
THE END