Implementation of Sorting Algorithms: Shell Sort, Radix Sort, and Quick Sort, Lab Reports of Data Structures and Algorithms

Solution Manual of lab no 9 of data structure and Algorithm

Typology: Lab Reports

2020/2021

Available from 01/15/2022

syed-afaq-hussain-shah
syed-afaq-hussain-shah 🇺🇸

14 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
18-EE-21
Lab # 09
Title:
Implementation of different Sorting Algorithms, Shell sort, Radix sort and Quick
Sort on array type data structures
Objective:
To Implementation of different Sorting Algorithms
Shell sort
Radix sort
Quick Sort
Equipment Required:
PC/ Laptop
Dev C++ software
Shell sort:
Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm. This algorithm
avoids large shifts as in case of insertion sort, if the smaller value is to the far right and has to be moved to
the far left. This algorithm uses insertion sort on a widely spread elements, first to sort them and then
sorts the less widely spaced elements. This spacing is termed as interval. This interval is calculated based
on Knuth's formula as:
Knuth's Formula:
h = h * 3 + 1
Where
h is interval with initial value 1
This algorithm is quite efficient for medium-sized data sets as its average and worst case complexity are of
Ο(n), where n is the number of items.
How Shell Sort Works?
Suppose, we need to sort the following array. Initial array
We are using the shell's original sequence (N/2, N/4, ...1) as intervals in our algorithm.
In the first loop, if the array size is N = 8 then, the elements lying at the interval of N/2 = 4 are compared
and swapped if they are not in order.
The 0th element is compared with the 4th element.
If the 0th element is greater than the 4th one then, the 4th element is first stored in temp variable and
the 0th element (i.e. greater element) is stored in the 4th position and the element stored in temp is
stored in the 0th position. Rearrange the elements at n/2 interval
1 | P a g e
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Implementation of Sorting Algorithms: Shell Sort, Radix Sort, and Quick Sort and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Lab # 09

 Title:

Implementation of different Sorting Algorithms, Shell sort, Radix sort and Quick

Sort on array type data structures

 Objective:

 To Implementation of different Sorting Algorithms  Shell sort  Radix sort  Quick Sort

 Equipment Required:

 PC/ Laptop  Dev C++ software

 Shell sort:

Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm. This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to the far right and has to be moved to the far left. This algorithm uses insertion sort on a widely spread elements, first to sort them and then sorts the less widely spaced elements. This spacing is termed as interval. This interval is calculated based on Knuth's formula as: Knuth's Formula: h = h * 3 + 1 Where h is interval with initial value 1 This algorithm is quite efficient for medium-sized data sets as its average and worst case complexity are of Ο(n), where n is the number of items.

How Shell Sort Works?

Suppose, we need to sort the following array. Initial array We are using the shell's original sequence (N/2, N/4, ...1) as intervals in our algorithm. In the first loop, if the array size is N = 8 then, the elements lying at the interval of N/2 = 4 are compared and swapped if they are not in order. The 0th element is compared with the 4th element. If the 0th element is greater than the 4th one then, the 4th element is first stored in temp variable and the 0th element (i.e. greater element) is stored in the 4th position and the element stored in temp is stored in the 0th position. Rearrange the elements at n/2 interval

This process goes on for all the remaining elements. Rearrange all the elements at n/2 interval In the second loop, an interval of N/4 = 8/4 = 2 is taken and again the elements lying at these intervals are sorted. Rearrange the elements at n/4 interval You might get confused at this point. All the elements in the array lying at the current interval are compared. The elements at 4th and 2nd position are compared. The elements at 2nd and 0th position are also compared. All the elements in the array lying at the current interval are compared. The same process goes on for remaining elements. Rearrange all the elements at n/4 interval Finally, when the interval is N/8 = 8/8 =1 then the array elements lying at the interval of 1 are sorted. The array is now completely sorted. Rearrange the elements at n/8 interval

 Quick sort:

Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays. A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value. Quick sort partitions an array and then calls itself recursively twice to sort the two resulting sub arrays. This algorithm is quite efficient for large-sized data sets as its average and worst case complexity are of Ο (n2), where n is the number of items.

How Quick Sort Works?

A pivot element is chosen from the array. You can choose any element from the array as the pivot element. Here, we have taken the rightmost (i.e. the last element) of the array as the pivot element. Select a pivot element

Swap pivot element with the second pointer Now the left and right subparts of this pivot element are taken for further processing in the steps below. Pivot elements are again chosen for the left and the right sub-parts separately. Within these sub-parts, the pivot elements are placed at their right position. Then, step 2 is repeated. Select pivot element of in each half and put at correct place using recursion The sub-parts are again divided into smaller sub-parts until each subpart is formed of a single element. At this point, the array is already sorted.

 Radix Sort:

We’ll close this chapter by briefly mentioning a sort that uses a different approach. The sorts we’ve looked at so far treat the key as a simple numerical value that is compared with other values to sort the data. The radix sort disassembles the key into digits and arranges the data items according to the value of the digits. Amazingly, no comparisons are necessary.

How Radix Sort Works?

Find the largest element in the array, i.e. max. Let X be the number of digits in max. X is calculated because we have to go through all the significant places of all elements. In this array [121, 432, 564, 23, 1, 45, 788], we have the largest number 788. It has 3 digits. Therefore, the loop should go up to hundreds place (3 times). Now, go through each significant place one by one. Use any stable sorting technique to sort the digits at each significant place. We have used counting sort for this. Sort the elements based on the unit place digits (X=0). Using counting sort to sort elements based on unit place

Now, sort the elements based on digits at tens place. Sort elements based on tens place Finally, sort the elements based on the digits at hundreds place. Sort elements based on hundreds place

LAB TASKS

1. Write a program that sort an array through shell sort? Also write its algorithm. Algorithm: Following is the algorithm for shell sort. Step 1 − Initialize the value of h = h * 3 + 1 Step 2 − Divide the list into smaller sub-list of equal interval h Step 3 − Sort these sub-lists using insertion sort Step 3 − Repeat until complete list is sorted Program Code: #include using namespace std; void sort(int a[],int n) { int gap,i,j,temp; for(gap=n/2;gap>0;gap/=2) { for(i=gap;i<n;i+=1) { temp=a[i]; for(j=i;j>=gap&&a[j-gap]>temp;j-=gap) a[j]=a[j-gap]; a[j]=temp; } } } int main() { int a[20],i,n; cout<<"\n\n\tEnter size of Array: "; cin>>n; cout<<"\n\t-------------------------------------------------\n"; for(i=0;i<n;++i) { cout<<"\n\t\tEnter "<<i+1<<" element: "; cin>>a[i]; } sort(a,n); cout<<"\n\t-------------------------------------------------\n"; cout<<"\n\tAfter shell sort, Sorted List is: ";

for(i=0;i<n;++i) { cout<<"\n\t\tEnter "<<i+1<<" element: "; cin>>a[i]; } quick_sort(a,0,n-1); cout<<"\n\t-------------------------------------------------\n"; cout<<"\n\tAfter Quick sort, Sorted List is: "; for(i=0;i<n;i++) { cout<<a[i]<<" "; } cout<<"\n\n\t-------------------------------------------------"; return 0; } void quick_sort(int a[],int l,int u) { int j; if(l<u) { j=partition(a,l,u); quick_sort(a,l,j-1); quick_sort(a,j+1,u); } } int partition(int a[],int l,int u) { int v,i,j,temp; v=a[l]; i=l; j=u+1; do { do i++; while(a[i]<v&&i<=u); do j--; while(v<a[j]); if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } }while(i<j); a[l]=a[j]; a[j]=v; return(j); }

Program Output:

3. Write a program that sort an array through radix sort? Also write its algorithm. Algorithm: Following is the algorithm for shell sort. Step 1 − All the data items are divided into 10 groups, according to the value of their 1s digit. Step 2 − These 10 groups are then reassembled: All the keys ending with 0 go first, followed by all the keys ending in 1, and so on up to 9. We’ll call these steps a sub-sort. Step 3 − In the second sub-sort, all data is divided into 10 groups again, but this time according to the value of their 10s digit. This must be done without changing the order of the previous sort. That is, within each of the 10 groups, the ordering of the items remains the same as it was after step 2; the sub-sorts must be stable. Step 4 − Again the 10 groups are recombined, those with a 10s digit of 0 first, then those with a 10s digit of 1, and so on up to 9. Step 5 − This process is repeated for the remaining digits. If some keys have fewer digits than others, their higher-order digits are considered to be 0. Here’s an example, using seven data items, each with three digits. Leading zeros are shown for clarity. Program Code: #include <stdio.h> int min = 0, count = 0, array[100] = {0}, array1[100] = {0}; int main() { int k, i, j, temp, t, n; printf("\n\n\tEnter size of array :"); scanf("%d", &count); printf("\n\t-------------------------------------------------\n"); for (i = 0; i < count; i++)