0% found this document useful (0 votes)
67 views7 pages

Data Structures Using C++ Rakesh

The document discusses various sorting algorithms in data structures: 1. It explains sorting as arranging records in a list or table according to a key value, either numerically or alphabetically in ascending or descending order. 2. It categorizes sorting into internal and external sorting based on whether all data fits in memory or not. 3. It provides examples and algorithms for insertion sort, merge sort, radix sort, and bucket/bin sort. Pseudocode and examples are given to illustrate how each algorithm sorts an array.

Uploaded by

Rakesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
67 views7 pages

Data Structures Using C++ Rakesh

The document discusses various sorting algorithms in data structures: 1. It explains sorting as arranging records in a list or table according to a key value, either numerically or alphabetically in ascending or descending order. 2. It categorizes sorting into internal and external sorting based on whether all data fits in memory or not. 3. It provides examples and algorithms for insertion sort, merge sort, radix sort, and bucket/bin sort. Pseudocode and examples are given to illustrate how each algorithm sorts an array.

Uploaded by

Rakesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Data Structures Using C++

Name – Rakesh Kumar

Roll no. 170970105018

EE 3 year

Q1) Explain sorting in data structure.

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

performed according to some key value of each record.

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.

Let's consider an array with values {5, 1, 6, 2, 4, 3}


Below, we have a pictorial representation of how bubble sort will sort the given array.

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 −

Now we should learn some programming aspects of merge sorting.

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.

Step 1 − if it is only one element in the list it is already sorted, return.


Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
Q4) Explain Radix sort with algorithm and example.
Ans. Radix sort or bucket sort is a method that can be used to sort a list of a number by its base. If we want to sort the list of English words, where
radix or base is 26 then 26 buckets are used to sort the words.

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.

Algorithm for Radix sort

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.

1. Input n number of elements in an array A.


2. Find the total number of digits in the largest element in the array.
3. Initialize i=1 and repeat the steps 4 and 5 until( i<=Digit).
4. Initialize the bucket j=0 and repeat the steps 5until (j<n).
th
5. Compare the i position of each element of the array with bucket number and place it in the corresponding bucket.
6. Read the elements (S) of the bucket from 0th bucket to 9th bucket and from the first position to the higher one to generate new array A.
7. Display the sorted array A.
8. Exit.

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.

The complexity of the Bucket Sort Technique

1. Time Complexity: O(n + k) for best case and average case and O(n^2) for the worst case.

2. Space Complexity: O(nk) for worst case

Input and Output


Input:
A list of unsorted data: 0.25 0.36 0.58 0.41 0.29 0.22 0.45 0.79 0.01 0.69
Array before Sorting: 0.25 0.36 0.58 0.41 0.29 0.22 0.45 0.79 0.01 0.69
Output:
Array after Sorting: 0.01 0.22 0.25 0.29 0.36 0.41 0.45 0.58 0.69 0.79
Algorithm
bucketSort(array, size)

Input: An array of data, and the total number in the array

Output: The sorted Array

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;

void display(float *array, int size) {


for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}

void bucketSort(float *array, int size) {


vector<float> bucket[size];
for(int i = 0; i<size; i++) { //put elements into different buckets
bucket[int(size*array[i])].push_back(array[i]);
}

for(int i = 0; i<size; i++) {


sort(bucket[i].begin(), bucket[i].end()); //sort individual vectors
}

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];
}

cout << "Array before Sorting: ";


display(arr, n);
bucketSort(arr, n);

cout << "Array after Sorting: ";


display(arr, n);
}
Output
Enter the number of elements: 10
Enter elements:
0.25 0.36 0.58 0.41 0.29 0.22 0.45 0.79 0.01 0.69
Array before Sorting: 0.25 0.36 0.58 0.41 0.29 0.22 0.45 0.79 0.01 0.69
Array after Sorting: 0.01 0.22 0.25 0.29 0.36 0.41 0.45 0.58 0.69 0.79

You might also like