Professional Documents
Culture Documents
Lab3 Dsa W23
Lab3 Dsa W23
1
Engr. Muhammad Arslan Rafique
Lab 03: Implementation of different Sorting Algorithms, Shell sort, Radix
sort and Quick Sort on array type data structures
Objective: To learn how to implement different sorting algorithms on an array type data
structure.
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.
We compare values in each sub-list and swap them (if necessary) in the original array. After
this step, the new array should look like this −
2
Engr. Muhammad Arslan Rafique
Then, we take interval of 2 and this gap generates two sub-lists - {14, 27, 35, 42}, {19, 10,
33, 44}
We compare and swap the values, if required, in the original array. After this step, the array
should look like this −
Finally, we sort the rest of the array using interval of value 1. Shell sort uses insertion sort to
sort the array.
3
Engr. Muhammad Arslan Rafique
We see that it required only four swaps to sort the rest of the array.
Algorithm
Following is the algorithm for shell sort.
Pseudocode
Following is the pseudocode for shell sort.
procedure shellSort()
A : array of items
/* calculate interval*/
while interval < A.length /3 do:
interval = interval * 3 + 1
end while
while interval > 0 do:
for outer = interval; outer < A.length; outer ++ do:
4
Engr. Muhammad Arslan Rafique
/* select value to be inserted */
valueToInsert = A[outer]
inner = outer;
/*shift element towards right*/
while inner > interval -1 && A[inner - interval] >= valueToInsert do:
A[inner] = A[inner - interval]
inner = inner - interval
end while
/* insert the number at hole position */
A[inner] = valueToInsert
end for
/* calculate interval*/
interval = (interval -1) /3;
end while
end procedure
Quicksort:
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
subarrays. 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.
Partition in Quick Sort
Following animated representation explains how to find the pivot value in an array.
Unsorted Array
The pivot value divides the list into two parts. And recursively, we find the pivot for each
sub-lists until all lists contains only one element.
5
Engr. Muhammad Arslan Rafique
Step 1 − Choose the highest index value has pivot
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot
while True do
while A[++leftPointer] < pivot do
//do-nothing
end while
end while
swap leftPointer,right
return leftPointer
6
Engr. Muhammad Arslan Rafique
end function
Radix Sort:
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.
Algorithm for the Radix Sort:
We’ll discuss the radix sort in terms of normal base-10 arithmetic, which is easier to
visualize. However, an efficient implementation of the radix sort would use base-2 arithmetic
to take advantage of the computer’s speed in bit manipulation. We’ll look at the radix sort
rather than the similar but somewhat more complex radix-exchange sort. The word radix
means the base of a system of numbers. Ten is the radix of the decimal system and 2 is the
radix of the binary system. The sort involves examining each digit of the key separately,
starting with the 1s (least significant) digit.
1. All the data items are divided into 10 groups, according to the value of their 1s digit.
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.
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
7
Engr. Muhammad Arslan Rafique
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.
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.
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.
Designing a Program:
In practice the original data probably starts out in an ordinary array. Where should the 10
groups go? There’s a problem with using another array or an array of 10 arrays. It’s not likely
there will be exactly the same number of 0s, 1s, 2s, and so on in every digit position, so it’s
hard to know how big to make the arrays. One way to solve this problem is to use 10 linked
lists instead of 10 arrays. Linked lists expand and contract as needed. We’ll use this approach.
An outer loop looks at each digit of the keys in turn. There are two inner loops: The first
takes the data from the array and puts it on the lists; the second copies it from the lists back to
the array. You need to use the right kind of linked list. To keep the sub-sorts stable, you need
the data to come out of each list in the same order it went in
Efficiency of the Radix Sort:
At first glance the efficiency of the radix sort seems too good to be true. All you do is copy
the original data from the array to the lists and back again. If there are 10 data items, this is
20 copies. You repeat this procedure once for each digit. If you assume, say, 5-digit numbers,
then you’ll have 20*5 equals 100 copies. If you have 100 data items, there are 200*5 equals
1,000 copies. The number of copies is proportional to the number of data items, which is
O(N), the most efficient sorting algorithm we’ve seen.
Unfortunately, it’s generally true that if you have more data items, you’ll need longer keys. If
you ave 10 times as much data, you may need to add another digit to the key. The number of
copies is proportional to the number of data items times the number of digits in the key. The
number of digits is the log of the key values, so in most situations we’re back to O(N*logN)
efficiency, the same as quicksort.
There are no comparisons, although it takes time to extract each digit from the number. This
must be done once for every two copies. It may be, however, that a given computer can do
the digit-extraction in binary more quickly than it can do a comparison. Of course, like
mergesort, the radix sort uses about twice as much memory as quicksort.
8
Engr. Muhammad Arslan Rafique
9
Engr. Muhammad Arslan Rafique
Lab Tasks:
1. Write a program that sort an array through shell sort? Also write its algorithm.
INPUT:
OUTPUT:
ALGOTITHM:
1. Start with a gap value (usually half the length of the array).
2. Divide the array into smaller subarrays of size gap.
3. For each subarray, sort the elements using Insertion Sort.
4. Repeat step 2 and 3 with a smaller gap value until the gap value is 1.
5. Apply the Insertion Sort to the whole array.
As Shell Sort is a variation of Insertion Sort, where instead of comparing adjacent elements,
elements at a distance of gap are compared and swapped.
10
Engr. Muhammad Arslan Rafique
2. Write a program that sort an array through quick sort? Also write its algorithm.
INPUT:
OUTPUT:
ALGORITHM:
11
Engr. Muhammad Arslan Rafique
3. Write a program that sort an array through radix sort? Also write its algorithm.
INPUT:
OUTPUT:
ALGORITHM:
1. Find the maximum number in the array to determine the number of digits required for
the largest number.
2. For each digit place (starting from the least significant to the most significant), use
counting sort to sort the array based on the digit at that place.
3. Repeat step 2 for each digit place until the entire number is sorted.
12
Engr. Muhammad Arslan Rafique
Lab Assessment
Understanding Ability to Generation and Ability to
of Engineering perform Interpretation of relate
Tools experiment using results using experiment
(Criteria 1) Engineering Tool modern tools with theory and Total
2 (Dev C++) (Dev C++) its significance 15
(Criteria 2) (Criteria 3) (Criteria 4) Marks
6 4 3
Task 1
Task 2
Task 3
Average
Marks
13
Engr. Muhammad Arslan Rafique