0% found this document useful (0 votes)
15 views26 pages

Sorting

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

Sorting

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

Sorting

The process of arranging an array in ascending or descending order is called


sorting.

If the elements are numbers, then they can be sorted in ascending or descending
order.

In Case the elements are characters or Strings then you can sort them in
alphabetical order.

Bubble Sort
Bubble sort is the simplest sorting algorithm that sorts an array of items in multiple
passes.

In the first pass, first and second item are compared and swapped(exchanged) if
they are in the wrong order. Then the second and third items are compared and
swapped if they are in the wrong order.

This process continues until the last two items of an array are compared and
swapped if they are in the wrong order.

In the first pass, n-1 comparisons occur. After the first pass, the first largest item is
placed at the first last position.

In the second pass, n-2 comparisons occur. After the second pass, the second
largest item is placed at the second last position.

After n-1 passes, entire array is sorted and hence no more items are left for
comparison.

Implementation of Bubble Sort

Illustrates how the following numbers, get sorted in ascending order using
bubble sort:- {8,7,9,2}

First Pass
Second Pass

After 2nd pass second largest number 8 bubbles to second last position.

Third pass
This array is sorted after three passes.

Bubble Sort
Algorithm
BUBBLE_SORT(a,n)

Step 1: Repeat for i =0 to less than n-1

Step 2: Repeat for j= 0 to less than n-1-i

if a[j]>a[j+1] then

set temp = a[j]

set a[j] = a[j+1]

set a[j+1] = temp

[End of if structure]

[End of Step 2 loop]

[End of Step1 loop]

/*A PROGRAM TO SORT AN ARRAY IN ASCENDING ORDER USING BUBBLE


SORT*/

#include<stdio.h>

void BUBBLE_SORT(int a[],int n)


{

int i,j,temp;

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

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

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

void DISPLAY(int a[], int n)

int i;

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

printf("%d\t ", a[i]);

int main()

{
int a[15],n,i;

printf("How many values to sort?:");

scanf("%d",&n);

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

printf("\nEnter value:");

scanf("%d",&a[i]);

BUBBLE_SORT(a,n);

printf("\nAfter sorting,array is\n");

DISPLAY(a,n);

return 0;

Selection Sort
If the array is to be arranged in ascending order, the smallest element need to be
selected and exchange/interchange with the first position element in the array.
Again, the smallest element from the remaining elements is selected and
exchange/interchange with the second array element. The process continues till all
elements are arranged.

If the array needs to be arranged in descending order, select the largest element.
Q. Illustrates how the following array, get sorted in ascending

order using Selection sort: {8,7,9,2,6,5}.

Sol.

1st pass

2nd pass

3rd pass
4th pass

5th pass

After fifth pass entire array gets sorted.

Selection Sort
Algorithm
SELECTION_SORT(a,n)

Step 1: Repeat for i =0 to less than n-1

Step 2: set min=a[i]

set pos=i

Step 3: Repeat for j= i+1 to n-1

if min>a[j] then

set min=a[j]

set pos=j

[End of if structure]

[End of Step 3 loop]

Step 4: if pos≠i then

set temp=a[pos]

set a[pos]=a[i]

set a[i]=temp

[End of if structure]

[End of Step1 loop]

/*A PROGRAM TO SORT AN ARRAY IN ASCENDING ORDER USING SELECTION


SORT*/

#include <stdio.h>

void SELECTION_SORT(int a[],int n)

int i,j,temp,min,pos;

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

min=a[i];

pos=i;

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

if(a[j]<min)

min=a[j];

pos=j;

if(pos!=i)

temp=a[pos];

a[pos]=a[i];

a[i]=temp;

void DISPLAY(int a[], int n)

int i;

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

printf("%d\t ", a[i]);


}

int main()

int a[15],n,i;

printf("How many values to sort?:");

scanf("%d",&n);

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

printf("\nEnter value:");

scanf("%d",&a[i]);

SELECTION_SORT(a,n);

printf("\nAfter sorting,array is\n");

DISPLAY(a,n);

return 0;

Insertion Sort
Insertion sort is a simple sorting algorithm that works similar to the way you sort
playing cards in your hands. The array is virtually split into a sorted and an
unsorted part. Values from the unsorted part are picked and placed at the correct
position in the sorted part.
Insertion Sort

Algorithm
INSERTION_SORT(a,n)

Step 1: Repeat for i = 1 to n-1

set key = a[i]

set j = i-1

Step 2: Repeat while j>=0 and key<a[j]

set a[j+1] = a[j]

set j = j-1

[End of Step 2 loop]

Step 3: set a[j+1] = key

[End of Step1 loop]

/*A PROGRAM TO SORT AN ARRAY IN ASCENDING ORDER USING INSERTION


SORT*/

#include <stdio.h>

void INSERTION_SORT(int a[],int n)

{
int i, j,key;

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

key = a[i];

j = i - 1;

while(j >= 0 &&key<a[j])

a[j + 1] = a[j];

j = j - 1;

a[j + 1] = key;

void DISPLAY(int a[], int n)

int i;

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

printf("%d\t ", a[i]);

int main()

int a[15],n,i;
printf("How many values to sort?:");

scanf("%d",&n);

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

printf("\nEnter value:");

scanf("%d",&a[i]);

INSERTION_SORT(a,n);

printf("\nAfter sorting,array is\n");

DISPLAY(a,n);

return 0;

Merging
Merging means combining the elements of two similar data structure into a third
data structure.

MERGING

Algorithm

MERGE(a,n1,b,n2,c)

Let a and b be two sorted arrays having n1 and n2 elements respectively.

This algorithm merge a and b in an array c with n=n1+n2 elements.

Step 1: Set i=0,j=0 and k=0

Step 2: Repeat while i<n1 and j<n2


[Compare]
If a[i]<=b[j] then
[Assign element from a to c]
set c[k]=a[i]
set i=i+1
else
[Assign element from b to c]
set c[k]=b[j]
set j=j+1
[End of if structure]
set k=k+1
[End of Step 2 loop]
Step 3: [Assign remaining elements from a to c]
Repeat while i<n1
set c[k]=a[i]
set i=i+1
set k=k+1
[End of Step 3 loop]
Step 4: [Assign remaining elements from b to c]
Repeat while j<n2
set c[k]=b[j]
set j=j+1
set k=k+1
[End of Step 4 loop]
Step 5: return k

/*A PROGRAM TO MERGE TWO SORTED ARRAYS*/

#include<stdio.h>

int MERGE(int a[],int n1,int b[],int n2,int c[])

int i,j,k;

i=j=k=0;

while(i<n1&&j<n2)

if(a[i]<=b[j])

c[k]=a[i];

i++;
}

else

c[k]=b[j];

j++;

k++;

while(i<n1)

c[k]=a[i];

i++;

k++;

while(j<n2)

c[k]=b[j];

j++;

k++;

return k;

}
void DISPLAY(int a[],int n)

int i;

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

printf("%d\t",a[i]);

int main()

int a[10],b[10],c[20],n1,n2,n,i,j;

printf("How many values in first array?:");

scanf("%d",&n1);

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

printf("\nEnter value:");

scanf("%d",&a[i]);

printf("\nHow many values in second array?:");

scanf("%d",&n2);

for(j=0;j<n2;j++)

printf("\nEnter value:");
scanf("%d",&b[j]);

n=MERGE(a,n1,b,n2,c);

printf("\nAfter merging,array is\n");

DISPLAY(c,n);

return 0;

Merge Sort
Merge sort is also based on divide and conquer approach. It divides the input array
into two sub-arrays and then recursively divide each of the two sub-arrays until we
have sub-arrays of length one. Finally, these sub-arrays are merged into one sorted
array.

In Merge sort the unsorted array is divided into n sub-arrays, each having one
element.
Merge Sort
Algorithm
Merge_Sort(a,beg,end)

Step1: if beg<end then


[Find the middle point to divide the array into two sub-arrays]

set mid = (beg+end)/2

[Call merge sort for first sub-array]

call Merge_Sort(a,beg,mid)

[Call merge sort for second sub-array]

call Merge_Sort(a,mid+1,end)

[Merge the two sorted sub-arrays]

call Merge(a,beg,mid,end)

[End of if structure]

Step 2: return

Merge
Algorithm
Merge(a,beg,mid,end)

Step 1: Set i=beg,j=mid+1 and k=0


Step 2: Repeat while i<=mid and j<=end
If a[i]<=a[j] then
set temp[k]=a[i]
set i=i+1
else
set temp[k]=a[j]
set j=j+1
[End of if structure]
set k=k+1
[End of Step 2 loop]
Step 3: Repeat while i<=mid
set temp[k]=a[i]
set i=i+1
set k=k+1
[End of Step 3 loop]
Step 4: Repeat while j<=end
set temp[k]=a[j]
set j=j+1
set k=k+1
[End of Step 4 loop]
Step 5: set k = 0
Step 6: [Copy elements from temp to a]
Repeat for i = beg to end
set a[i] = temp[k++]
[End of Step 6 loop]

/*A PROGRAM TO SORTS AN ARRAY IN ASCENDING ORDER USING MERGE


SORT*/

#include <stdio.h>

void MERGE(int a[],int beg,int mid,int end)

int i,j,k;

int n1=mid-beg+1,n2=end-mid;

int temp[n1+n2];

i=beg;

j=mid+1;

k=0;

while(i<=mid&&j<=end)

if(a[i]<=a[j])

temp[k]=a[i];

i++;
}

else

temp[k]=a[j];

j++;

k++;

while(i<=mid)

temp[k]=a[i];

i++;

k++;

while(j<=end)

temp[k]=a[j];

j++;

k++;

k=0;

for(i=beg;i<=end;i++)

a[i]=temp[k++];

void MERGE_SORT(int a[],int beg,int end)

{
int mid;

if(beg<end)

mid=(beg+end)/2;

MERGE_SORT(a,beg,mid);

MERGE_SORT(a,mid+1,end);

MERGE(a,beg,mid,end);

void DISPLAY(int a[], int n)

int i;

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

printf("%d\t ", a[i]);

int main()

int a[15],n,i;

printf("How many values to sort?:");

scanf("%d",&n);

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

printf("\nEnter value:");

scanf("%d",&a[i]);

MERGE_SORT(a,0,n-1);

printf("\nAfter sorting,array is\n");

DISPLAY(a,n);

return 0;

Quick Sort
Quick sort is based on divide and conquer approach. On the basis of a selected
element (pivot) from the list, it partitions the rest of the list into two parts—a sub-
list that contains elements less than the pivot and other sub-list containing
elements greater than the pivot. The pivot is inserted between the two sub-lists.
The algorithm is recursively applied to the sub-lists until the size of each sub-list
becomes 1, indicating that the whole list has become sorted.
Quick Sort
Algorithm
QUICK_SORT(a,beg,end)

Step1: if beg<end then

set mid = call Partition(a,beg,end)

call Quick_Sort(a,beg,mid-1)

call Quick_Sort(a,mid+1,end)

[End of if structure]

Step 2: return

PARTITION
Algorithm
PARTITION(a,beg,end)

Step 1: set pivot = a[end]

set i=beg-1

Step 2: Repeat for j=beg to end-1

If a[j]<=pivot then

set i=i+1

set temp=a[i]

set a[i]=a[j]

set a[j]=temp

[End of if structure]

[End of step 2 loop]

Step 3: set temp=a[i+1]

set a[i+1]=a[end]
set a[end]=temp

Step 4: return i+1

/*A PROGRAM TO SORT AN ARRAY IN ASCENDING ORDER USING QUICK


SORT*/

#include <stdio.h>

int PARTITION(int a[],int beg,int end)

int pivot,i,j,temp;

pivot=a[end];

i=beg-1;

for(j=beg;j<=end-1;j++)

if(a[j]<=pivot)

i++;

temp=a[i];

a[i]=a[j];

a[j]=temp;

temp=a[i+1];

a[i+1]=a[end];

a[end]=temp;

return i+1;
}

void QUICK_SORT(int a[],int beg,int end)

int mid;

if(beg<end)

mid=PARTITION(a,beg,end);

QUICK_SORT(a,beg,mid-1);

QUICK_SORT(a,mid+1,end);

void DISPLAY(int a[], int n)

int i;

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

printf("%d\t ", a[i]);

int main()

int a[15],n,i;

printf("How many values to sort?:");


scanf("%d",&n);

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

printf("\nEnter value:");

scanf("%d",&a[i]);

QUICK_SORT(a,0,n-1);

printf("\nAfter sorting,array is\n");

DISPLAY(a,n);

return 0;

You might also like