0% found this document useful (0 votes)
8 views8 pages

DS Notes Unit 02 Sorting

The document provides an overview of various sorting techniques used in programming, including Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, and Quick Sort. Each technique is explained with a brief description and accompanying C programming code to demonstrate how to implement the sorting algorithm. The document emphasizes the importance of sorting for efficient data arrangement within data structures.

Uploaded by

monalisa1190119
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

DS Notes Unit 02 Sorting

The document provides an overview of various sorting techniques used in programming, including Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, and Quick Sort. Each technique is explained with a brief description and accompanying C programming code to demonstrate how to implement the sorting algorithm. The document emphasizes the importance of sorting for efficient data arrangement within data structures.

Uploaded by

monalisa1190119
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Structures

Sorting Techniques
Definition
Sorting refers to arranging a group of elements in ascending, descending, alphabetical or some other
ordered manner. It is an important concept used in programming which allows efficient arrangement
of list of elements inside a data structure.

The sorting operation can be performed by using various techniques. Some of those techniques are:
1. Bubble sort
2. Insertion sort
3. Selection sort
4. Quick sort
5. Merge sort

Bubble Sort
It is one of the simple algorithm and it works on the bubble strategy. At the end of each pass (outer for
loop) the largest element is moved to the right most position of the array assuming that we wish to
sort in an ascending order. The technique applied in bubble sort is to compare the adjacent elements
starting from the beginning of the array. If the element on the left is greater than the element on the
right, then these two elements are swapped, otherwise no swapping is done.

Program to sort the given list using bubble sort technique


#include<stdio.h>
int main()
{
int a[20], n, i, j, temp;

printf("How many elements? ");


scanf("%d", &n);

printf("Enter the elements in unsorted manner: ");


for(i=0; i<n; i++)
scanf("%d", &a[i]);

for(i=0; i<n-1; i++)


for(j=0; j<n-i-1; j++)
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}

printf("Sorted elements are\n");


for(i=0; i<n; i++)
printf("%d\n", a[i]);

return 0;
}

KLS GCC BCA Unit 2 – Sorting Techniques Page 1 | 8


Data Structures
Insertion Sort
Insertion sort allows placing the smallest elements at leftmost position at every iteration (outer for
loop). Starting from the second element it compares with the first element and swap it if it is not in
order. Similarly we take the 3rd element in the next iteration and place it at the right place in the sub
array.

Program to sort the given list using insertion sort technique.

#include<stdio.h>

int main()
{
int a[20], n, i, j, temp;

printf("How many elements? ");


scanf("%d", &n);

printf("Enter the elements in unsorted manner: ");


for(i=0; i<n; i++)
scanf("%d", &a[i]);

for(i=1; i<n; i++)


{
temp=a[i];
for(j=i; (j>0 && a[j-1]>temp); j--)
a[j]=a[j-1];
a[j]=temp;
}

printf("Sorted array is: \n");


for(i=0; i<n; i++)
printf("%d\n", a[i]);

return 0;
}

KLS GCC BCA Unit 2 – Sorting Techniques Page 2 | 8


Data Structures
Selection Sort
In this sorting algorithm, the list is divided into two parts the sorted part at the left and the unsorted
part at the right end. Initially the sorted part is empty and the unsorted part is the entire list. The
smallest element is selected from the unsorted array and swapped with the leftmost element and the
element becomes a part of the sorted array. This process continues moving unsorted array boundary
by 1 element to the right.

Program to sort the given list using selection sort technique.

#include<stdio.h>

int main()
{
int a[20], n, i, j, min, temp;

printf("How many elements? ");


scanf("%d", &n);

printf("Enter the elements: ");


for(i=0; i<n; i++)
scanf("%d", &a[i]);

for(i=0; i<n; i++)


{
min = i;
for(j=i+1; j<n; j++)
{
if(a[min] > a[j])
min = j;
}
if(min!=i)
{
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}

printf("The sorted array in ascending sort: \n");


for(i=0; i<n; i++)
printf("%d\n", a[i]);

return 0;
}

KLS GCC BCA Unit 2 – Sorting Techniques Page 3 | 8


Data Structures
Merge Sort
It is an important technique that merges two sorted data structures into a single one.

Consider two arrays A and B in which elements are already sorted as given below:

A = [5, 15, 35] B = [10, 20, 30, 40, 50]

The objective of merge sort is to merge elements of A and B into a single array C. Let i, j and k be the
variables used to keep track of elements in array A, B, and C respectively. We compare the elements in
each array and the smaller of them is added to C. If the element in array A is smaller, then i and k
variables are incremented. If the element in array B is smaller, then j and k variables are incremented.
This process is continued till either of one array get exhausted. If A gets exhausted first then the
remaining items of B are simply appended to C otherwise vice-versa. This method is also called as
simple merge or two-way merge sort.

A = [5, 15, 35] B = [10, 20, 30, 40, 50] C = []

A = [15, 35] B = [10, 20, 30, 40, 50] C = [5]

A = [15, 35] B = [20, 30, 40, 50] C = [5, 10]

A = [35] B = [20, 30, 40, 50] C = [5, 15]

A = [35] B = [30, 40, 50] C = [5, 15, 20]

A = [35] B = [40, 50] C = [5, 15, 20, 30]

A = [] B = [40, 50] C = [5, 15, 20, 30, 35]

A = [] B = [] C = [5, 15, 20, 30, 35, 40, 50]

Program to sort the given list using merge sort technique.


#include<stdio.h>

int main()
{
int a[10], b[10], c[20], m, n, i, j, k;

printf("How many elements in first array (A)? ");


scanf("%d", &m);

printf("Enter the elements of array A in sorted order: ");


for(i=0; i<m; i++)
scanf("%d", &a[i]);

printf("How many elements in second array (B)? ");


scanf("%d", &n);

printf("Enter the elements of array B in sorted order: ");


for(j=0; j<n; j++)
scanf("%d", &b[j]);

i=j=k=0;
while( (i<m) && (j<n) )
{

KLS GCC BCA Unit 2 – Sorting Techniques Page 4 | 8


Data Structures
if(a[i] < b[j])
{
c[k] = a[i];
i++;
k++;
}
else
{
c[k] = b[j];
j++;
k++;
}
}

// Append the remaining elements of A


while(i<m)
{
c[k] = a[i];
i++;
k++;
}

// Append the remaining elements of B


while(j<n)
{
c[k]=b[j];
j++;
k++;
}

printf("The sorted array after merging:\n");


for(i=0;i<(m+n);i++)
printf("%d\n", c[i]);

return 0;
}

KLS GCC BCA Unit 2 – Sorting Techniques Page 5 | 8


Data Structures
Quick Sort
1. It is a method for sorting an array efficiently.
2. It was developed by C.A.R. Hoare and is also known as partition exchange sort.
3. In quick sort, we assume the first element given in the array as the pivot and remaining n-1
elements are compared with the pivot.
4. We maintain two indices i and j.
Hence, pivot = a[0]
i=1
j = n-1
5. The pivot is compared with the a[i] until an element whose value is greater than pivot is
encountered. At this time/point, we stop incrementing ‘i'.
6. Now, the pivot is compared with the a[j] until an element whose value is less than pivot is
encountered. At this time/point, we stop decrementing ‘j’.
7. If i<j, then swap a[i] and a[j].
8. This process is repeated until i<j.
9. If i>j, then swap pivot and a[j].
10. Observe that all the elements on the left of j are smaller than pivot and the elements on the
right of j are greater than pivot.
11. The array is now divided into two sub-arrays.

left sub array right sub array

Less than pivot pivot Greater than pivot

12. Now the left sub-array and right-sub array are individually sorted by calling quicksort
recursively.

Program to sort the given list using quick sort technique.


#include<stdio.h>

void quicksort(int[], int, int);


int partition(int [], int, int);

int main()
{
int a[20], n, i, j, low, high;

printf("How many elements? ");


scanf("%d", &n);

printf("Enter the elements in unsorted manner: ");


for(i=0; i<n; i++)
scanf("%d", &a[i]);

low = 0;
high = n-1;
quicksort(a, low, high);
KLS GCC BCA Unit 2 – Sorting Techniques Page 6 | 8
Data Structures

printf("The sorted list is: \n");


for(i=0; i<n; i++)
printf("%d\n", a[i]);

return 0;
}

void quicksort(int a[], int low, int high)


{
int j;
if(low<high)
{
j = partition(a, low, high);
quicksort(a, low, j-1);
quicksort(a, j+1, high);
}
}

int partition(int a[], int low, int high)


{
int i, j, pivot, temp, flag;
pivot = a[low];
i = low+1;
j = high;
flag = 1;

while(flag == 1)
{
while( (a[i]<pivot) && (i<j) )
i = i+1;

while(a[j]>pivot)
j = j-1;

if(i<j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
flag=0;
}
temp = a[low];
a[low] = a[j];
a[j] = temp;

return(j);
}
KLS GCC BCA Unit 2 – Sorting Techniques Page 7 | 8
Data Structures
Program explanation
Consider the unsorted elements: 25, 10, 72, 18, 40, 11, 32, 9

Step 1: Indices 0 1 2 3 4 5 6 7
Elements 25 10 72 18 40 11 32 9
Pivot i j

[Remember, i and j contain indices, whereas pivot contains the array element]

Step 2: increment i and decrement j


25 10 72 18 40 11 32 9
Pivot i j

Since, i<j is true, swap ith element with jth element.


25 10 9 18 40 11 32 72
Pivot i j

Step 3: increment i and decrement j


25 10 9 18 40 11 32 72
Pivot i j

Since, i<j is true swap ith element with jth element.


25 10 9 18 11 40 32 72
Pivot i j

Step 4: increment i and decrement j


25 10 9 18 11 40 32 72
Pivot j i

Since, i<j is false, swap pivot and a[j]


11 10 9 18 25 40 32 72
Pivot j i

Step 5: Now, return the j and make the partition.

Step 6: Recursively sort the left and right sub arrays.


Left sub array → 11 10 9 18
Right sub array → 40 32 72

11 10 9 18 25 40 32 72
Pivot i j Pivot i j

Step 7: Increment i and decrement j


11 10 9 18 25 40 32 72
Pivot j i Pivot j i

Step 8: Since, i<j is false, swap pivot and a[j]. Return j and make the partition.
9 10 11 18 25 32 40 72

The process is repeated to get the sorted array:


9 10 11 18 25 32 40 72

KLS GCC BCA Unit 2 – Sorting Techniques Page 8 | 8

You might also like