Symbiosis Skills and Professional University
B. Tech in Computer Science & IT (Cyber Security)
Skill-4
CSIT201: Data Structures and Algorithms
B.Tech Semester: II
Academic Year: 2021-2022 (Odd Sem.)
Prepared By:
Nirbhay Patil_2101106081
2101106081 Nirbhay Patil
Skill 4
Aim: Write a C Program to Implement following :
1 Bubble Sort
Pseudo code/Algorithm:
1. [ Initialize]
LAST <- N
2. [ loop on pass index]
Repeat thru step 5 for pass = 1,2,…,N-1
3. [ Initialize exchanges counter for this pass ]
EXCHS <- 0
4. [ Perform pair wise comparisons on unsorted elements ]
Repeat for I = 1, 2,….., LAST - 1
if K[I] > K[I + 1]
then K[I] < > K[I + 1]
EXCHS < - EXCHS + 1
5. [Were any exchanges made on this pass ? ]
if EXCHS = 0
then Return
else
LAST < - LAST - 1
6.[ finish ]
Exit
C Program:
// C program for implementation of Bubble sort
#include <stdio.h>
/*
* Main Function
*/
int main()
{
int n, j, i, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
int array[n];
printf("Enter %d integers\n", n);
for (i= 0; i < n; i++)
{
2101106081 Nirbhay Patil
scanf("%d", &array[i]);
}
for (i = 0 ; i < n - 1; i++)
{
for (j = 0 ; j < n - i- 1; j++)
{
if (array[j] > array[j+1])
{
swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
printf("%d\n", array[i]);
return 0;
}
2101106081 Nirbhay Patil
2.Insertion Sort
Pseudo code/Algorithm:
1. [ loop on index]
Repeat thru step 4 for I = 1,…,N-1
2. [ Initialize variable]
TEMP <- K[I]
J <- I -1
3. [ Perform comparisons ]
Repeat while J>=0 && TEMP < K[J]
K[J+1] <- K[J]
J <- J – 1
4. [ Place value in proper position ]
K[J+1] <- TEMP
5. [ Finish]
Exit
C Program:
#include <stdio.h>
int main()
{
int n, i, j, temp;
int arr[64];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1 ; i <= n - 1; i++)
{
j = i;
while ( j > 0 && arr[j-1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i <= n - 1; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
2101106081 Nirbhay Patil
3.Selection Sort
Pseudo code/Algorithm:
1. [ loop on pass index]
Repeat thru step 4 for pass = 1,2,…,N-1
2. [ Initialize minimum index]
MIN_INDEX <- PASS
3. [Make a pass and obtain element with smallest value]
Repeat for I = PASS+1, PASS+2,….., N
if K[I] < K[MIN_INDEX]
then MIN_INDEX <- I
4. [Exchange elements]
if MIN_INDEX ≠ PASS
then K[PASS] < > K[MIN_INDEX]
5.[ finish ] Exit
C Program:
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d Numbersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
2101106081 Nirbhay Patil
{
swap=a[i];
a[i]=a[position];
a[position=swap;
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", a[i]);
return 0;
}
4.merge sort
Pseudo code/Algorithm:
2101106081 Nirbhay Patil
1. [ Initialize]
I <- FIRST
J <- SECOND
L <- 0
2. [ Compare corresponding elements and output the smallest]
Repeat while I < SECOND and J <= THIRD
if K[I] <= K[J]
then L <- L + 1
TEMP[L] <- K[I]
I <- I + 1
else
L <- L + 1
TEMP [L] <- K[J]
J <- J + 1
3. [ Copy the remaining unprocessed elements in output area]
if I >= SECOND
then repeat while J <= THIRD
L <- L +1
TEMP [L] <- K[J]
J <- J +1
else repeat while I < SECOND
L <- L +1
TEMP[L] <- K[I]
I <- I +1
4. [ Copy elements in temporary vector into original area ]
Repeat for I = 1, 2,….., L
K[FIRST -1 +I] <- TEMP[I]
5. [ Finished ]
exit
C Program:
2101106081 Nirbhay Patil
#include <stdio.h>
void mergeSort(int [], int, int, int);
void partition(int [],int, int);
int main()
{
int list[50];
int i, size;
printf("Enter total number of elements:");
scanf("%d", &size);
printf("Enter the elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
partition(list, 0, size - 1);
printf("After merge sort:\n");
for(i = 0;i < size; i++)
{
printf("%d ",list[i]);
}
return 0;
}
void partition(int list[],int low,int high)
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
partition(list, low, mid);
partition(list, mid + 1, high);
mergeSort(list, low, mid, high);
}
}
void mergeSort(int list[],int low,int mid,int high)
{
int i, mi, k, lo, temp[50];
lo = low;
i = low;
mi = mid + 1;
while ((lo <= mid) && (mi <= high))
{
if (list[lo] <= list[mi])
{
temp[i] = list[lo];
lo++;
}
else
{
2101106081 Nirbhay Patil
temp[i] = list[mi];
mi++;
}
i++;
}
if (lo > mid)
{
for (k = mi; k <= high; k++)
{
temp[i] = list[k];
i++;
}
}
else
{
for (k = lo; k <= mid; k++)
{
temp[i] = list[k];
i++;
}
}
for (k = low; k <= high; k++)
{
list[k] = temp[k];
}
}
5.Quick Sort
Pseudo code/Algorithm:
2101106081 Nirbhay Patil
1. [ Initialize ]
FLAG <- true
2. [ Perform sort ]
if LB < UB
then I <- LB
J <- UB + 1
KEY <- K[LB]
repeat while FLAG
I <- I + 1
repeat while K[I] < KEY && I < UB
I<- I + 1
J <- J -1
repeat while K[J] > KEY
J <- J -1
if I < J
then K[I] < > k[J]
else
FLAG <- false
K[LB] < > K[J]
call QUICK_SORT(K, LB, J-1)
call QUICK_SORT(K, J + 1, UB)
7. [ Finish]
Exit
C Program:
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
2101106081 Nirbhay Patil
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
2101106081 Nirbhay Patil