-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 314ef26
Showing
6 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Data Structures | ||
|
||
|
||
|
||
|
||
This is a repository containing various C++ Programs to understand the basic concepts of Data Structures and Algorithms. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <iostream> | ||
using namespace std; | ||
void Bubblesort(int A[],int N) | ||
{ | ||
int i,j,t; | ||
for(i=0;i<=N-2;i++) | ||
for(j=0;j<=N-2-i;j++) | ||
if(A[j]>A[j+1]) | ||
{ | ||
t=A[j]; | ||
A[j]=A[j+1]; | ||
A[j+1]=t; | ||
} | ||
} | ||
int main() | ||
{ | ||
int A[200],N,i; | ||
cout<<"Enter no. of elements to be taken: "; | ||
cin>>N; | ||
cout<<"Enter the elements of array: "<<endl; | ||
for(i=0;i<N;i++) | ||
cin>>A[i]; | ||
Bubblesort(A,N); | ||
cout<<"Sorted array: "<<endl; | ||
for(i=0;i<N;i++) | ||
cout<<A[i]<<" "; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <iostream> | ||
using namespace std; | ||
void Insertionsort(int A[],int N) | ||
{ | ||
int i,j,t; | ||
for(i=1;i<=N-1;i++) | ||
{ | ||
t=A[i]; | ||
j=i-1; | ||
while(t<A[j]&&j>=0) | ||
{ | ||
A[j+1]=A[j]; | ||
j--; | ||
} | ||
A[j+1]=t; | ||
} | ||
} | ||
int main() | ||
{ | ||
int A[200],N,i; | ||
cout<<"Enter total no. of elements to be sorted: "; | ||
cin>>N; | ||
cout<<"Enter the elements of array: "<<endl; | ||
for(i=0;i<N;i++) | ||
cin>>A[i]; | ||
Insertionsort(A,N); | ||
cout<<"Sorted array: "<<endl; | ||
for(i=0;i<N;i++) | ||
cout<<A[i]<<" "; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <iostream> | ||
using namespace std; | ||
void merging(int A[],int l,int m,int r) | ||
{ | ||
int i,j,k,n1,n2; | ||
n1=m-l+1; | ||
n2=r-m; | ||
int L[n1],R[n2]; | ||
for(i=0;i<n1;i++) | ||
L[i]=A[l+i]; | ||
for(j=0;j<n2;j++) | ||
R[j]=A[m+1+j]; | ||
i=0; | ||
j=0; | ||
k=l; | ||
while(i<n1&&j<n2) | ||
{ | ||
if(L[i]<=R[j]) | ||
{ | ||
A[k]=L[i]; | ||
i++; | ||
} | ||
else | ||
{ | ||
A[k]=R[j]; | ||
j++; | ||
} | ||
k++; | ||
} | ||
while(i<n1) | ||
{ | ||
A[k]=L[i]; | ||
i++; | ||
k++; | ||
} | ||
while(j<n2) | ||
{ | ||
A[k]=R[j]; | ||
j++; | ||
k++; | ||
} | ||
} | ||
void mergesort(int A[],int l,int r) | ||
{ | ||
if(l<r) | ||
{ | ||
//Same as (l+r)/2, but avoids overflow for large l and h | ||
int m=l+(r-l)/2; | ||
mergesort(A,l,m); | ||
mergesort(A,m+1,r); | ||
merging(A,l,m,r); | ||
} | ||
} | ||
int main() | ||
{ | ||
int A[200],N,i; | ||
cout<<"Enter total no. of elements to be sorted: "; | ||
cin>>N; | ||
cout<<"Enter the elements of array: "<<endl; | ||
for(i=0;i<N;i++) | ||
cin>>A[i]; | ||
mergesort(A,0,N-1); | ||
cout<<"Sorted array: "<<endl; | ||
for(i=0;i<N;i++) | ||
cout<<A[i]<<" "; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <iostream> | ||
using namespace std; | ||
void swap1(int &a,int &b) | ||
{ | ||
int t; | ||
t=a; | ||
a=b; | ||
b=t; | ||
} | ||
int partition1(int A[],int l,int h) | ||
{ | ||
int pivot=A[h]; | ||
int i=l-1; | ||
for(int j=l;j<=h-1;j++) | ||
{ | ||
if(A[j]<=pivot) | ||
{ | ||
i++; | ||
swap1(A[i],A[j]); | ||
} | ||
} | ||
swap1(A[i+1],A[h]); | ||
return (i+1); | ||
} | ||
void quicksort(int A[],int l,int h) | ||
{ | ||
if(l<h) | ||
{ | ||
int p=partition1(A,l,h); | ||
quicksort(A,l,p-1); | ||
quicksort(A,p+1,h); | ||
} | ||
} | ||
int main() | ||
{ | ||
int A[200],N,i; | ||
cout<<"Enter total no. of elements to be sorted: "; | ||
cin>>N; | ||
cout<<"Enter the elements of array: "<<endl; | ||
for(i=0;i<N;i++) | ||
cin>>A[i]; | ||
quicksort(A,0,N-1); | ||
cout<<"Sorted array: "<<endl; | ||
for(i=0;i<N;i++) | ||
cout<<A[i]<<" "; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <iostream> | ||
using namespace std; | ||
void Selectionsort(int A[],int n) | ||
{ | ||
int i,j,small,pos,temp; | ||
for(i=0;i<=n-2;i++) | ||
{ | ||
small=A[i]; | ||
pos=i; | ||
for(j=i+1;j<=n-1;j++) | ||
if(A[j]<small) | ||
{ | ||
small=A[j]; | ||
pos=j; | ||
} | ||
temp=A[i]; | ||
A[i]=A[pos]; | ||
A[pos]=temp; | ||
} | ||
} | ||
int main() | ||
{ | ||
int A[200],N,i; | ||
cout<<"Enter total no. of elements to be sorted: "; | ||
cin>>N; | ||
cout<<"Enter the elements of array: "<<endl; | ||
for(i=0;i<N;i++) | ||
cin>>A[i]; | ||
Selectionsort(A,N); | ||
cout<<"Sorted array: "<<endl; | ||
for(i=0;i<N;i++) | ||
cout<<A[i]<<" "; | ||
return 0; | ||
} |