0% found this document useful (0 votes)
46 views

Algorithm PDF

The document discusses and provides code implementations for several sorting algorithms - bubble sort, insertion sort, selection sort, merge sort, quick sort, and heap sort. For each algorithm, it provides the time complexity analysis for best, worst and average cases as well as space complexity and stability.

Uploaded by

jenny khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Algorithm PDF

The document discusses and provides code implementations for several sorting algorithms - bubble sort, insertion sort, selection sort, merge sort, quick sort, and heap sort. For each algorithm, it provides the time complexity analysis for best, worst and average cases as well as space complexity and stability.

Uploaded by

jenny khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Bubble sort :

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,i,j,k,l,temp;
cout<<"enter number of elements: ";
cin>>n;
int x[n];
for(i=0;i<n;i++)
{
cin>>x[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;

}
}
}
cout<<"the sorted array is: "<<endl;
for(i=0;i<n;i++)
{
cout<<x[i]<<" ";
}
cout<<endl;

return 0;
}

/// Best case --- O(n)


///wrost case --- O(n^2)
///average case --- O(n^2)
///Space Complexity --- O(1)
///Stability ---- YES
Insertion sort :
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,i,j,k,l,temp;
cout<<"enter number of elements: ";
cin>>n;
int x[n];
for(i=0;i<n;i++)
{
cin>>x[i];
}

for(i=1;i<n;i++)
{
m=x[i];
j=i-1;
while(j>=0 && x[j]>m)
{
x[j+1]=x[j];
j--;
}
x[j+1]=m;
}

cout<<"the sorted array is: "<<endl;


for(i=0;i<n;i++)
{
cout<<x[i]<<" ";
}
cout<<endl;

return 0;
}

/// Best case --- O(n)


///wrost case --- O(n^2)
///average case --- O(n^2)
///Space Complexity --- O(1)
///Stability ---- YES
Selection sort :
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,i,j,k,l,temp;
cout<<"enter number of elements: ";
cin>>n;
int x[n];
for(i=0;i<n;i++)
{
cin>>x[i];
}

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

cout<<"the sorted array is: "<<endl;


for(i=0;i<n;i++)
{
cout<<x[i]<<" ";
}
cout<<endl;
return 0;
}
/// Best case --- O(n^2)

///wrost case --- O(n^2)

///average case --- O(n^2)

///Space Complexity --- O(1)

///Stability ---- NO
Merge sort :
#include<bits/stdc++.h>
using namespace std;

void merge(int x[],int l ,int m ,int r)


{
int i,j,k;
int n1,n2;
n1=m-l+1;
n2=r-m;
int L[n1],R[n2];
for(i=0;i<n1;i++)
{
L[i]=x[l+i];
}
for(i=0;i<n2;i++)
{
R[i]=x[m+1+i];
}
i=0;
j=0;
k=l;
while(i<n1 && j<n2)
{
if(L[i]<=R[j])
{
x[k]=L[i];
i++;
}
else
{
x[k]=R[j];
j++;
}
k++;
}
while(i<n1)
{
x[k]=L[i];
i++;
k++;
}
while(j<n2)
{
x[k]=R[j];
j++;
k++;
}

void merge_sort(int x[],int l,int r)


{
if(r>l)
{
int m=l+(r-l)/2;
merge_sort(x,l,m);
merge_sort(x,m+1,r);
merge(x,l,m,r);
}
}
int main()
{
int n,m,i,j,k,l,temp;
cout<<"enter number of elements: ";
cin>>n;
int x[n];
for(i=0;i<n;i++)
{
cin>>x[i];
}
merge_sort(x,0,n-1);
cout<<"the sorted array is: "<<endl;
for(i=0;i<n;i++)
{
cout<<x[i]<<" ";
}
cout<<endl;

return 0;
}

/// Best case --- O(n*log n)


///wrost case --- O(n*log n)
///average case --- O(n*log n)
///Space Complexity --- O(n)
///Stability ---- YES
Quick sort :
#include<bits/stdc++.h>
using namespace std;
void swap(int* a,int* b)
{
int t=*a;
*a=*b;
*b=t;
}
int partition(int x[],int l,int r)
{
int i,j,k,pivot;
i=l-1;
pivot=x[r];
for(j=l;j<r;j++)
{
if(x[j]<=pivot)
{
i++;
swap(&x[i],&x[j]);
}
}
swap(&x[i+1],&x[r]);
return i+1;
}
void quick_sort(int x[],int l,int r)
{
if(l<r)
{
int pivot = partition(x,l,r);
quick_sort(x,l,pivot-1);
quick_sort(x,pivot+1,r);
}
}
int main()
{
int n,m,i,j,k,l,temp;
cout<<"enter number of elements: ";
cin>>n;
int x[n];
for(i=0;i<n;i++)
{
cin>>x[i];
}
quick_sort(x,0,n-1);

cout<<"the sorted array is: "<<endl;


for(i=0;i<n;i++)
{
cout<<x[i]<<" ";
}
cout<<endl;

return 0;
}

/// Best case --- O(n*log n)


///wrost case --- O(n^2)
///average case --- O(n*log n)
///Space Complexity --- O(log n)
///Stability ---- NO
Heap sort :
#include<bits/stdc++.h>
using namespace std;

void heapify(int x[],int n,int i)


{
int largest = i;
int l= 2*i + 1;
int r= 2*i + 2;

if(l<n && x[l]>x[largest])


{
largest=l;
}
if(r<n && x[r]>x[largest])
{
largest = r;
}
if(largest != i)
{
int temp = x[i];
x[i]=x[largest];
x[largest]=temp;

heapify(x,n,largest);
}

void heap_sort(int x[],int n)


{
int i;
for(i=(n/2)-1;i>=0;i--)
{
heapify(x,n,i);
}
for(i=n-1;i>0;i--)
{
int temp = x[i];
x[i]=x[0];
x[0]=temp;
heapify(x,i,0);
}
}

int main()
{
int n,m,i,j,k,l,temp;
cout<<"enter number of elements: ";
cin>>n;
int x[n];
for(i=0;i<n;i++)
{
cin>>x[i];
}

heap_sort(x,n);

cout<<"the sorted array is: "<<endl;


for(i=0;i<n;i++)
{
cout<<x[i]<<" ";
}
cout<<endl;

return 0;
}

/// Best case --- O(n log n)


///wrost case --- O(n log n)
///average case --- O(n log n)
///Space Complexity --- O(1)
///Stability ---- NO

You might also like