Data Structures Using C++ Rakesh
Data Structures Using C++ Rakesh
EE 3 year
Ans.Sorting refers to the operation or technique of arranging and rearranging sets of data in some specific order. A collection of records called a list
where every record has one or more fields. The fields which contain a unique value for each record is termed as the key field. For example, a phone
number directory can be thought of as a list where each record has three fields - 'name' of the person, 'address' of that person, and their 'phone
numbers'. Being unique phone number can work as a key to locate any record in the list.
Sorting is the operation performed to arrange the records of a table or list in some order according to some specific ordering criterion. Sorting is
The records are either sorted either numerically or alphanumerically. The records are then arranged in ascending or descending order depending on
the numerical value of the key. Here is an example, where the sorting of a lists of marks obtained by a student in any particular subject of a class.
Categories of Sorting-----
The techniques of sorting can be divided into two categories. These are:
Internal Sorting
External Sorting
Internal Sorting: If all the data that is to be sorted can be adjusted at a time in the main memory, the internal sorting method is being performed.
External Sorting: When the data that is to be sorted cannot be accommodated in the memory at the same time and some has to be kept in auxiliary
memory such as hard disk, floppy disk, magnetic tapes etc, then external sorting methods are performed.
Q 2) Explain Insertion sorts with algorithm and example.
Ans.Following are the steps involved in insertion sort:
1. We start by making the second element of the given array, i.e. element at index 1, the key. The key element here is the new card that we
need to add to our existing sorted set of cards(remember the example with cards above).
2. We compare the key element with the element(s) before it, in this case, element at index 0:
o If the key element is less than the first element, we insert the key element before the first element.
o If the key element is greater than the first element, then we insert it after the first element.
3. Then, we make the third element of the array as key and will compare it with elements to it's left and insert it at the right position.
4. And we go on repeating this, until the array is sorted.
As you can see in the diagram above, after picking a key, we start iterating over the elements to the left of the key.
We continue to move towards left if the elements are greater than the key element and stop when we find the element which is less than
the key element.
And, insert the key element after the element which is less than the key element.
Algorithm---
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the
value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
Q3) Explain merge sort with algorithm and example.
Ans.To understand merge sort, we take an unsorted array as the following −
We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array
of 8 items is divided into two arrays of size 4.
This does not change the sequence of appearance of items in the original. Now we divide these two arrays into halves.
We further divide these arrays and we achieve atomic value which can no more be divided.
Now, we combine them in exactly the same manner as they were broken down. Please note the color codes given to these lists.
We first compare the element for each list and then combine them into another list in a sorted manner. We see that 14 and 33 are in sorted positions.
We compare 27 and 10 and in the target list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42 and 44 are
placed sequentially.
In the next iteration of the combining phase, we compare lists of two data values, and merge them into a list of found data values placing all in a
sorted order.
After the final merging, the list should look like this −
Algorithm
Merge sort keeps on dividing the list into equal halves until it can no more be divided. By definition, if it is only one element in the list, it is sorted.
Then, merge sort combines the smaller sorted lists keeping the new list sorted too.
To sort an array of decimal number where the radix or base is 10 we need 10 buckets and can be numbered as 0,1,2,3,4,5,6,7,8,9. A number of
passes required to have a sorted array depend upon the number of digits in the largest element.
Let A be a linear array of n elements A[1], A[2], A[3]............A[n]. Digit is the total number of digit in the largest element in array A.
Example: To illustrate the radix sort consider the following array with 7 elements
42,133,7,23,74,670,49
In this array, the biggest elements are 670 and the number of digits is 3, so 3 passes are required to sort the array. Read the elements and compare
the first position (.2 is in the first position of 42) digits with the digits of the buckets and place it.
Now read the elements from left to right and the bottom to the top of the bucket and place it in an array for the next pass. Read the array elements and
compare the second position (4 is in the second position of the elements 042) digit with a number of the bucket and place it.
Again read the element from left to right and from bottom to top to get an array for the third pass. (0 is in the first position of 042) compare the third
position digit in each element with the budget digit and place it wherever it matches.
Now the sorted array is 7 , 23, 42, 49, 74, 133, 670.
Q 5) Explain bin sort with algorithm.
Ans.Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets.
In the Bucket Sorting technique, the data items are distributed in a set of buckets. Each bucket can hold a similar type of data. After distributing, each
bucket is sorted using another sorting algorithm. After that, all elements are gathered on the main list to get the sorted form.
1. Time Complexity: O(n + k) for best case and average case and O(n^2) for the worst case.
Begin
for i := 0 to size-1 do
insert array[i] into the bucket index (size * array[i])
done
for i := 0 to size-1 do
sort bucket[i]
done
for i := 0 to size -1 do
gather items of bucket[i] and put in array
done
End
Source Code (C++)
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int index = 0;
for(int i = 0; i<size; i++) {
while(!bucket[i].empty()) {
array[index++] = *(bucket[i].begin());
bucket[i].erase(bucket[i].begin());
}
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
float arr[n]; //create an array with given number of elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}