Created
March 18, 2020 10:43
-
-
Save ra101/ca28ffc04ea4e83face032a124464688 to your computer and use it in GitHub Desktop.
diff sorting algos on std::list in STL lib C++
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
#include<iostream> | |
#include<list> | |
#include<iterator> | |
using namespace std; | |
list<int>::iterator itr1; | |
list<int>::iterator itr2; | |
void merge(list<int>&,list<int>::iterator,list<int>::iterator,list<int>::iterator); | |
void mergeSort(list<int>&); | |
void mergeSortHelper(list<int>&,list<int>::iterator,list<int>::iterator); | |
int n,i,j,k; | |
void bubbleSort(list<int>&); | |
void selectionSort(list<int>&); | |
void insertionSort(list<int>&); | |
void shellSort(list<int>&); | |
void countSort(list<int>&); | |
void radixSort(list<int>&); | |
void countSortForRadix(list<int>&,int); | |
int getMaxElement(list<int>&); | |
int main() | |
{ | |
cout<<"Enter the no. of Elements: "; | |
cin>>n; | |
cout<<"Enter the Elements: "; | |
list<int> arr(n); | |
list<int>::iterator itr; | |
for (itr=arr.begin();itr!=arr.end();itr++) | |
{ | |
cin>>*itr; | |
} | |
mergeSort(arr); | |
printf("Sorted Array: "); | |
for (itr=arr.begin();itr!=arr.end();itr++) | |
{ | |
cout<<*itr<<" "; | |
} | |
return 0; | |
} | |
int getMaxElement(list<int>& arr) | |
{ | |
int max=-((2^((sizeof(int)*8)-1))); | |
for (itr1=arr.begin();itr1!=arr.end();itr1++) | |
{ | |
if(*itr1>max) | |
{ | |
max=*itr1; | |
} | |
} | |
return max; | |
} | |
int getMinElement(list<int>& arr) | |
{ | |
int min=((2^((sizeof(int)*8)-1))-1); | |
for (itr1=arr.begin();itr1!=arr.end();itr1++) | |
{ | |
if(*itr1<min) | |
{ | |
min=*itr1; | |
} | |
} | |
return min; | |
} | |
void countSort(list<int>& arr) | |
{ | |
list<int> sortedArr(arr.size()); | |
int n = getMaxElement(arr)- getMinElement(arr) + 2; | |
int countArr[n]={0};//from 0 to max; | |
for (itr1=arr.begin();itr1!=arr.end();itr1++) | |
{ | |
++countArr[*itr1-1]; | |
} | |
for(i=1;i<n;i++) | |
{ | |
countArr[i]+=countArr[i-1]; | |
} | |
for (itr1=arr.begin();itr1!=arr.end();itr1++) | |
{ | |
*next(sortedArr.begin(),countArr[*itr1-1]-1)=*itr1; | |
--countArr[*itr1-1]; | |
} | |
arr=sortedArr; | |
} | |
void radixSort(list<int>& arr) | |
{ | |
int max = getMaxElement(arr); | |
for (int digit=1;max/digit>0;digit*=10) | |
{ | |
countSortForRadix(arr,digit); | |
} | |
} | |
void countSortForRadix(list<int>& arr,int digit) | |
{ | |
list<int> sortedArr(arr.size()); | |
int countArr[10]={0}; | |
for (itr1=arr.begin();itr1!=arr.end();itr1++) | |
{ | |
countArr[(*itr1/digit)%10]++; | |
} | |
for(i=1;i<10;i++) | |
{ | |
countArr[i]+=countArr[i-1]; | |
} | |
for (list<int>::reverse_iterator ritr = arr.rbegin(); ritr != arr.rend(); ++ritr) | |
{ | |
*next(sortedArr.begin(),(countArr[(*ritr/digit)%10]-1))=*ritr; | |
countArr[(*ritr/digit)%10]--; | |
} | |
arr=sortedArr; | |
} | |
void bubbleSort(list<int>& arr) | |
{ | |
for (itr1=arr.begin();itr1!=arr.end();itr1++) | |
{ | |
for (itr2=next(itr1,1);itr2 != prev(arr.end(),1);itr2++) | |
{ | |
if (*itr2 > *next(itr2,1)) | |
{ | |
*itr2^=*next(itr2,1)^=*itr2^=*next(itr2,1); | |
} | |
} | |
} | |
} | |
void insertionSort(list<int>& arr) | |
{ | |
int temp; | |
for (itr1=next(arr.begin(),1);itr1!=arr.end();itr1++) | |
{ | |
itr2=prev(itr1,1); | |
temp = *itr1; | |
while((*itr2)>=0 && temp<*itr2) | |
{ | |
*next(itr2,1)=*itr2; | |
--itr2; | |
} | |
*next(itr2,1)=temp; | |
} | |
} | |
void shellSort(list<int>& arr) | |
{ | |
int gap; | |
gap=(arr.size())/2; | |
while(gap>0) | |
{ | |
for(itr1=next(arr.begin(),gap);itr1!=arr.end();itr1++) | |
{ | |
for(itr2=itr1;distance(arr.begin(),itr2)>=gap && *itr2<*prev(itr2,gap);itr2=prev(itr2,gap)) | |
{ | |
*itr2^=*prev(itr2,gap)^=*itr2^=*prev(itr2,gap); | |
} | |
} | |
gap=gap/2; | |
} | |
} | |
void selectionSort(list<int>& arr) | |
{ | |
for (itr1=arr.begin();itr1!=prev(arr.end(),1);itr1++) | |
{ | |
for (itr2=next(itr1,1);itr2!=arr.end();itr2++) | |
{ | |
if (*itr2<*itr1) | |
{ | |
*itr1^=*itr2^=*itr1^=*itr2; | |
} | |
} | |
} | |
} | |
void mergeSort(list<int>& arr) | |
{ | |
itr1=arr.begin(); | |
itr2=prev(arr.end(),1); | |
mergeSortHelper(arr,itr1,itr2); | |
} | |
void mergeSortHelper(list<int>& arr,list<int>::iterator itr1,list<int>::iterator itr2) | |
{ | |
list<int>::iterator mid = next(arr.begin(),((distance(arr.begin(),itr1)+distance(arr.begin(),itr2))/2)); | |
if(distance(arr.begin(),itr1)<distance(arr.begin(),itr2)) | |
{ | |
mergeSortHelper(arr,itr1,mid); | |
mergeSortHelper(arr,next(mid,1),itr2); | |
merge(arr,itr1,mid,itr2); | |
} | |
} | |
void merge(list<int>& arr,list<int>::iterator itr1,list<int>::iterator mid,list<int>::iterator itr2) | |
{ | |
list<int> new_arr; | |
list<int>::iterator itri=itr1; | |
list<int>::iterator itrj=next(mid,1); | |
list<int>::iterator itrk=new_arr.begin(); | |
while(itri!=next(mid,1) && itrj!=next(itr2,1)) | |
{ | |
if(*itri<*itrj) | |
{ | |
*itrk=*itri; | |
itri++; | |
} | |
else | |
{ | |
*itrk=*itrj; | |
itrj++; | |
} | |
itrk++; | |
} | |
while(itri!=next(mid,1)) | |
{ | |
*itrk=*itri; | |
itri++; | |
itrk++; | |
} | |
while(itrj!=next(itr2,1)) | |
{ | |
*itrk=*itrj; | |
itrj++; | |
itrk++; | |
} | |
for(itri=itr1,itrk=new_arr.begin();itri!=next(itr2,1);itri++,itrk++) | |
{ | |
*itri=*itrk; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment