You are on page 1of 16

INDEX

S.No. NAME OF PRACTICAL DATE SIGNATURE

1 Write a program to implement Insertion Sort. 25/08/17

2 Write a program to implement Selection Sort. 01/09/17

3 Write a program to implement Bubble Sort. 08/09/17

4 Write a program to implement Quick Sort. 15/09/17

5 Write a program to implement Merge Sort. 22/09/17

6 Write a program to implement Heap Sort. 29/09/17

7 Write a program to implement Counting Sort. 06/10/17

8 Write a program to implement Radix or Bucket Sort. 13/10/17

9 Write a program for Recursive Binary & Linear 20/10/17


Search.

10 Study of NP-Complete Theory. 27/10/17

11 Study of Cook's theorem. 03/11/17

12 Study of Sorting Network. 10/11/17

13
Experiment No. 1
Write a program to implement Insertion Sort.

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

printf("Enter the total no. of terms: ");


scanf("%d",&n);

printf("Enter values of elements:");

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

for(j=1;j<n;j++)
{
key=a[j];
i=j-1;

while(i>=0 && a[i]>key)


{
a[i+1]= a[i];
i=i-1;
}

a[i+1]= key;

printf("Sorted Array is:");


for(i=0;i<n;i++)
printf("%d ", a[i]);
getch();
}
Output-

Eperiment No. 2

Write a program to implement Selection Sort.

#include <stdio.h>

int main()
{
int array[100], n, c, d, position, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

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


scanf("%d", &array[c]);

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


{
position = c;
for ( d = c + 1 ; d < n ; d++ )
{
if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}

printf("Sorted list in ascending order:\n");

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


printf("%d\n", array[c]);

return 0;
}

Output-
Experiment No. 3

Write a program to implement Bubble Sort.

#include<stdio.h>
int main(){

int s,temp,i,j,a[20];

printf("Enter total numbers of elements: ");


scanf("%d",&s);

printf("Enter %d elements: ",s);


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

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

printf("After sorting: ");


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

return 0;
}
Output-

Experiment No. 4

Write a program to implement Quick Sort.

#include <stdio.h>

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

int main()
{
int a[50],n,i;
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:");

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

quick_sort(a,0,n-1);
printf("\nArray after sorting:");
for(i=0;i<n;i++)
printf("%d ",a[i]);

return 0;
}

void quick_sort(int a[],int l,int u)


{
int j;
if(l<u)
{
j=partition(a,l,u);
quick_sort(a,l,j-1);
quick_sort(a,j+1,u);
}
}

int partition(int a[],int l,int u)


{
int v,i,j,temp;
v=a[l];
i=l;
j=u+1;

do
{
do
i++;

while(a[i]<v&&i<=u);

do
j--;
while(v<a[j]);

if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);

a[l]=a[j];
a[j]=v;

return(j);
}
Output-

Experiment No. 5

Write a program to implement Merge Sort.

#include <iostream>

using namespace std;

void Merge(int *a, int low, int high, int mid)


{
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;

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


{
if (a[i] < a[j])
{
temp[k] = a[i];
k++;
i++;
}
else
{
temp[k] = a[j];
k++;
j++;
}
}

while (i <= mid)


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

while (j <= high)


{
temp[k] = a[j];
k++;
j++;
}

for (i = low; i <= high; i++)


{
a[i] = temp[i-low];
}
}

void MergeSort(int *a, int low, int high)


{
int mid;
if (low < high)
{
mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);

Merge(a, low, high, mid);


}
}

int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}

MergeSort(arr, 0, n-1);

cout<<"\nSorted Data ";


for (i = 0; i < n; i++)
cout<<"->"<<arr[i];

return 0;
}

Output-

Experiment No. 6

Write a program to implement Heap Sort.

#include <iostream>

using namespace std;


void MaxHeapify(int a[], int i, int n)
{
int j, temp;
temp = a[i];
j = 2*i;

while (j <= n)
{
if (j < n && a[j+1] > a[j])
j = j+1;
if (temp > a[j])
break;
else if (temp <= a[j])
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = temp;
return;
}
void HeapSort(int a[], int n)
{
int i, temp;
for (i = n; i >= 2; i--)
{
temp = a[i];
a[i] = a[1];
a[1] = temp;
MaxHeapify(a, 1, i - 1);
}
}
void Build_MaxHeap(int a[], int n)
{
int i;
for(i = n/2; i >= 1; i--)
MaxHeapify(a, i, n);
}
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
n++;
int arr[n];
for(i = 1; i < n; i++)
{
cout<<"Enter element "<<i<<": ";
cin>>arr[i];
}
// Building max heap.
Build_MaxHeap(arr, n-1);
HeapSort(arr, n-1);

// Printing the sorted data.


cout<<"\nSorted Data ";

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


cout<<"->"<<arr[i];

return 0;
}

Output-

Experiment No. 7

Write a program to implement Counting Sort.

#include <iostream>

using namespace std;


void SelectionSort (int arr[], int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = i+1; j < n; ++j)
{
if (arr[i] > arr[j])
{
arr[i] = arr[i]+arr[j];
arr[j] = arr[i]-arr[j];
arr[i] = arr[i]-arr[j];
}
}
}
}

int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;

int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}

SelectionSort(arr, n);

// Display the sorted data.


cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];

return 0;
}
Output-

Experiment No. 8

Write a program to implement Radix or Bucket Sort.

#include <iostream>

using namespace std;

int getMax(int arr[], int n)


{
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}

void countSort(int arr[], int n, int exp)


{
int output[n], i, count[10] = {0};

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


count[(arr[i] / exp) % 10]++;
for (i = 1; i < 10; i++)
count[i] += count[i-1];

for (i = n - 1; i >= 0; i--)


{
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}

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


arr[i] = output[i];
}

void radixsort(int arr[], int n)


{
int exp, m;
m = getMax(arr, n);

for (exp = 1; m/exp > 0; exp *= 10)


countSort(arr, n, exp);
}

int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;

int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}

radixsort(arr, n);

// Printing the sorted data.


cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}
Output-

You might also like