0% found this document useful (0 votes)
65 views9 pages

Program 1: Write A Programto Implement Bubble Sort. Source Code

The document discusses 6 different sorting algorithms: bubble sort, selection sort, insertion sort, radix sort, merge sort, and quick sort. Code snippets are provided to implement each algorithm. The code takes user input of array sizes and values, performs the sorting algorithm, and outputs the sorted array.

Uploaded by

hardik bhalodia
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)
65 views9 pages

Program 1: Write A Programto Implement Bubble Sort. Source Code

The document discusses 6 different sorting algorithms: bubble sort, selection sort, insertion sort, radix sort, merge sort, and quick sort. Code snippets are provided to implement each algorithm. The code takes user input of array sizes and values, performs the sorting algorithm, and outputs the sorted array.

Uploaded by

hardik bhalodia
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/ 9

Program 1: Write a programto implement bubble sort.

Source Code:
#include<iostream>
using namespace std;

int main()
{
int a[10],i,j,t;

for(i=0;i<10;i++)
{
cout<<" \nEnter number "<<i<<" : ";
cin>>a[i];
}

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

for(i=0;i<10;i++)
{
cout<<" "<<a[i];
}
return 0;
}

Output:

2.) Write a program to implement Selection sort.


Source Code:
#include <iostream>
using namespace std
void selection_sort(int[],int)
int main()
{
int x,y;
cout<<"\nEnter Size of Array : ";
cin>>x;
int a[x];
for(int i=0;i<x;i++)
{
cout<<"\nENter Number : ";
cin>>a[i];
}
selection_sort(a,x);
cout<<"\n\nImplementing Selection Sort, Sorted array is : ";
for(int i=0;i<x;i++)
{ cout<<" "<<a[i]; }
return 0;
}
void selection_sort(int a[],int n)
{
int t,min,temp;
for(int i=0;i<n;i++)
{
min=a[i];
t=i;
for(int j=i;j<n;j++)
{
if(a[j]<min)
{
min=a[j];
t=j;
} }
temp=a[i];
a[i]=min;
a[t]=temp;
}}
Output:

3.) Write a program to implement Insertion sort.


Source Code:
#include <iostream>
using namespace std;
void insertion_sort(int[],int);

int main()
{
int x,y;
cout<<"\nEnter Size of Array : ";
cin>>x;
int a[x];
for(int i=0;i<x;i++)
{
cout<<"\nENter Number : ";
cin>>a[i];
}
insertion_sort(a,x);
cout<<"\n\nImplementing Insertion Sort, Sorted array is : ";
for(int i=0;i<x;i++)
{
cout<<" "<<a[i];
}
return 0;
}

void insertion_sort(int a[],int n)


{
int t,min,temp;
for(int i=0;i<n;i++)
{
for(int j=i;a[j]<a[j-1];j--)
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
Output:

4.) Write a program to implement Radix sort:


Source Code:
#include <iostream>
#include <iterator>
using namespace std;

void radix_sort(int[],int);
void count_sort(int[],int,int);
int max_value(int[],int);

int main()
{
int x,y;
cout<<"\nEnter Size of Array : ";
cin>>x;
int a[x];
for(int i=0;i<x;i++)
{
cout<<"\nENter Number : ";
cin>>a[i];
}
radix_sort(a,x);
cout<<"\n\nImplementing Radix Sort, Sorted array is : ";
for(int i=0;i<x;i++)
{
cout<<" "<<a[i];
}
return 0;
}

void radix_sort(int a[],int n)


{
int pvalue,max;
max = max_value(a,n);

for (pvalue = 1; max/pvalue > 0; pvalue *= 10)


{
count_sort(a,n,pvalue);
}
}

int max_value(int a[],int x)


{
int max=a[0];
for(int i=0;i<x;i++)
{
if(a[i]>max)
{
max=a[i];
}

}
return max;
}
void count_sort(int a[],int n,int pvalue)
{
int temp[n], i;
int count[10] = {0}; // acts as a bucket, to count occurence

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


{
count[(a[i] / pvalue) % 10]++;
}

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


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

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


{
temp[count[(a[i] / pvalue) % 10] - 1] = a[i];
count[(a[i] / pvalue) % 10]--;
}

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


{
a[i] = temp[i];
}
}
Output:

5.) Write a program to implement Merge sort.


Source Code:
#include <iostream>
using namespace std;
void merge_sort(int[],int);
void merge(int[],int[],int,int[],int);

int main()
{
int x,y,l;
cout<<"\nEnter Size of Array : ";
cin>>x;
int a[x];
for(int i=0;i<x;i++)
{
cout<<"\nENter Number : ";
cin>>a[i];
}
merge_sort(a,x);

cout<<"\n\nImplementing Merge Sort, Sorted array is : ";


for(int i=0;i<x;i++)
{
cout<<" "<<a[i];
}
return 0;
}

void merge_sort(int a[],int x)


{
int mid,i;
if(x<2)
{ return;}
else
{
mid=x/2;
int left[mid];
int right[x-mid];
for(i=0;i<mid;i++)
{
left[i]=a[i];
}
for(i=mid;i<x;i++)
{
right[i-mid]=a[i];
}
merge_sort(left,mid);
merge_sort(right,x-mid);
merge(a,left,mid,right,x-mid);
}
}

void merge(int a[],int l[],int ll,int r[],int lr)


{
int i=0,j=0,k=0;
while(i<ll && j<lr)
{
if(l[i]<=r[j])
{
a[k]=l[i];
i++;
}
else
{
a[k]=r[j];
j++;
}
k++;
}
while(i<ll)
{
a[k]=l[i];
k++; i++;
}
while(j<lr)
{
a[k]=r[j];
j++; k++;
}
}
Output:

6.) Write a program to implement Quick sort.


Source Code:
#include <iostream>
using namespace std;
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
{
int x,y;
cout<<"\nEnter Size of Array : ";
cin>>x;
int a[x];
for(int i=0;i<x;i++)
{
cout<<"\nENter Number : ";
cin>>a[i];
}
quick_sort(a,0,x-1);
cout<<"\n\nImplementing Quick Sort, Sorted array is : ";
for(int i=0;i<x;i++)
{
cout<<" "<<a[i];
}
return 0;
}

void quick_sort(int a[],int start,int end)


{
if(start<end)
{
int pos=partition(a,start,end);
quick_sort(a,start,pos-1);
quick_sort(a,pos+1,end);
}
}

int partition(int a[],int start,int end)


{
int pivot=a[end];
int pos=start;
int temp;
for(int i=start;i<end;i++)
{
if(a[i]<=pivot)
{
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
pos++;
}
}
temp=a[pos];
a[pos]=a[end];
a[end]=temp;
return pos;
}
Output:

You might also like