Output:
The sorted array is:
23
23
23
34
45
65
67
89
90
101
Complexity of Quick Sort
Pass 1 will have n comparisons. Pass 2 will have 2*(n/ 2) comparisons. In the subsequent
passes will have 4*(n/4), 8*(n/8) comparisons and so on. The total comparisons involved
in this case would be O(n)+O(n)+O(n)+···+s. The value of expression will be O (n log
n).Thus time complexity of quick sort is O(n log n).Space required by quick sort is very
less, only O(n log n) additional space is required.
2.2.6 RADIX SORT
Radix sort is one of the sorting algorithms used to sort a list of integer numbers in order.
In radix sort algorithm, a list of integer numbers will be sorted based on the digits of
individual numbers. Sorting is performed from least significant digit to the most
significant digit.
Radix sort algorithm requires the number of passes which are equal to the number of
digits present in the largest number among the list of numbers. For example, if the largest
number is a 3 digit number then that list is sorted with 3 passes.
Step by Step Process
The Radix sort algorithm is performed using the following steps...
Step 1 - Define 10 queues each representing a bucket for each digit from 0 to 9.
Step 2 - Consider the least significant digit of each number in the list which is to
be sorted.
Step 3 - Insert each number into their respective queue based on the least
significant digit.
Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have
inserted into their respective queues.
Step 5 - Repeat from step 3 based on the next least significant digit.
Step 6 - Repeat from step 2 until all the numbers are grouped based on the most
significant digit.
Algorithm for Radix Sort
Algorithm for RadixSort (ARR, N)
Step 1: Find the largest number in ARR as LARGE
Step 2: [INITIALIZE] SET NOP = Number of digits in LARGE
Step 3: SET PASS =
Step 4: Repeat Step 5 while PASS <= NOP-1
Step 5:SET I = and INITIALIZE buckets
Step 6:Repeat Steps 7 to 9 while I<N-1
Step 7:SET DIGIT = digit at PASSth place in A[I]
Step 8:Add A[I] to the bucket numbered DIGIT
Step 9:INCEREMENT bucket count for bucket numbered DIGIT
[END OF LOOP]
Step 1 :Collect the numbers in the bucket
[END OF LOOP]
Step 11: END
Example Sort the numbers given below using radix sort.
345, 654, 924, 123, 567, 472, 555, 808, 911
In the first pass, the numbers are sorted according to the digit at ones place. The
buckets are pictured upside down as shown below.
After this pass, the numbers are collected bucket by bucket. The new list thus formed is
used as an input for the next pass. In the second pass, the numbers are sorted according to
the digit at the tens place. The buckets are pictured upside down.
In the third pass, the numbers are sorted according to the digit at the hundreds
place. The buckets are pictured upside down.
The numbers are collected bucket by bucket. The new list thus formed is the
final sorted result. After the third pass, the list can be given as
123, 345, 472, 555, 567, 654, 808, 911, 924.
Advantages:
o Radix sort algorithm is well known for its fastest sorting algorithm for
numbers and even for strings of letters.
o Radix sort algorithm is the most efficient algorithm for elements which
are arranged in descending order in an array.
Disadvantages:
o Radix sort takes more space than other sorting algorithms.
o Poor efficieny for most elements which are already arranged in
ascending order in an array.
o When Radix sort algorithm is applied on very small sets of data(numbers
or strings), then algorithm runs in 0(n) asymptotic time.
25. int arr[30];
26. int k, num;
Complexity of Radix Sort:
27. printf("Enter total no. of elements : ");
28. To sort an unsorted
scanf("%d", &num); list with 'n' number of elements,
29. Radix sort algorithm
printf("\nEnter needs ",the
%d numbers: num);
following complexities...
30.
31. for (k = 0 ; k < num; k++)
32. Worst
{ Case : O(n)
33. Best
scanf("%d",: &arr[k]);
Case O(n)
Average Case : O(n)
34. }
35. shellsort(arr, num);
36. printf("\n Sorted array is: ");
37. for (k = 0; k < num; k++)
38. printf("%d ", arr[k]);
39. return 0;
40. }
Output:
Enter total no. of elements : 6
Enter 6 numbers: 3
2
4
10
2
1
Sorted array is: 1 2 2 3 4 10
Complexity of Shell Sort
Time complexity of above implementation of shellsort is O(n2). In the above
implementation, gap is reduced by half in every iteration.
2.2.7 TABLE OF COMPARISON OF ALL SORTING TECHNIQUES
Time complexity of various sorting algorithms
S.No. Algorithm Worst Case Average Case Best Case
1. Selection Sort O (n2) O (n2) O (n2)
2. Bubble Sort O (n2) O (n2) O (n2)
3. Insertion Sort O (n2) O (n2) n–1
4. Quick Sort O (n2) log2 n log2 n
5. Heap Sort O (n log n) O (n log n) O (n log n)
6. Merge Sort O (n log n) O (n log n) O (n log n)
7. Radix Sort O (n2) O (n log n) O (n log n)
8. Shell sort O(n log2 n) – –
2.3 SUMMERY
Sorting deals with sorting the data stored in the memory, whereas external sorting
deals with sorting the data stored in files.
In bubble sorting, consecutive adjacent pairs of elements in the array are compared
with each other.
Insertion sort works by moving the current data element past the already sorted values
and repeatedly interchanging it with the preceding value until it is in the correct place.
Selection sort works by finding the smallest value and placing it in the first position.
It then finds the second smallest value and places it in the second position. This
procedure is repeated until the whole array is sorted.
Merge sort is a sorting algorithm that uses the divide, conquer, and combine
algorithmic paradigm. Divide means partitioning the n-element array to be sorted into
two sub-arrays of n/2 elements in each sub-array. Conquer means sorting the two sub-
arrays recursively using merge sort. Combine means merging the two sorted sub-
arrays of size n/2 each to produce a sorted array of n elements. The running time of
merge sort in average case and worst case can be given as O(n log n).
Quick sort works by using a divide-and-conquer strategy. It selects a pivot element
and rearranges the elements in such a way that all elements less than pivot appear
before it and all elements greater than pivot appear after it.
Radix sort is a linear sorting algorithm that uses the concept of sorting names in
alphabetical order.
Heap sort sorts an array in two phases. In the first phase, it builds a heap of the given
array. In the second phase, the root element is deleted repeatedly and inserted into an
array.