diff --git a/sorting/1.cpp b/sorting/1.cpp new file mode 100644 index 0000000..72a3211 --- /dev/null +++ b/sorting/1.cpp @@ -0,0 +1,79 @@ +//InPlace-Algorithm....... + + + +//Bubble Sort +//This algorithm is used to swap two adjacent elements one by one untill and unless we get the sorted array. +//Stable + +#include +using namespace std; + +//Time Complexity is O(n**2)........ + +void bubble_sort(int *a,int n) +{ + for(int i=0;ia[j+1]){ + swap(a[j],a[j+1]); + temp=false; + } + } + if(temp) + break; + } + +} +//Selection sort +//This Algorthim is used to find the min index at every iteration and swap the current element with the minimum element. +//Unstable +//Time Complexity is O(n**2)........... + +void selection_sort(int *a,int n) +{ + for(int i=0;ia[j]) + min_index=j; + } + swap(a[i],a[min_index]); + } +} +//Insertion sort +//This algortith is used to place the current element at correct position and then it will place all lesser element before the current element. +//stable........ + +//Time Complexity is O(n**2)...... +void insertion_sort(int *a,int n) +{ + for(int i=1;i=0 && a[j]>temp;j--) + a[j+1]=a[j]; + a[j+1]=temp; + } +} +int main() +{ + int a[]={5,4,3,2,1}; + bubble_sort(a,5); + for(int i=0;i<5;i++) + cout< +using namespace std; + +//Method 1 +//Naive-Implementation..... + +//Time Complexity is O(n+k)................... + +void count_sort_1(int *a,int n,int k) +{ + int temp[k]={0}; + int index=0; + for(int i=0;i=0;i--) + { + output[count[a[i]-1]]=a[i]; + count[a[i]]--; + } + for(int i=0;i +using namespace std; + +void bucket_sort(int *a,int total_bucket,int n) +{ + int m=a[0]; + for(int i=1;ibuckets[total_bucket]; + for(int i=0;i +using namespace std; +//Radix sort +//This algorthim is the extended version of counting sort because the data may be is in the range of 1 to k**2,that's why we use this algorithm insteed of using Counting sort. +//In this algorithm we will sort the array acc.to digits. +//We will first sort it acc to least significant digit ,then sort it acc to middle digit and finally sort it acc to most significant digit. +//The whole logic is similar to counting sort...... + +//Time Complexity is Theta(d*(n+b))........ +//b is the base +//here b=10; +//Auxiliary space is Theta(n+b).......... + +//counting-sort +void counting_sort(int *a,int n,int exp) +{ + int count[10]={0}; + for(int i=0;i=0;i--) + { + output[count[(a[i]/exp)%10]-1]=a[i]; + count[(a[i]/exp)%10]--; + } + for(int i=0;i0;exp=exp*10) + { + counting_sort(a,n,exp); + } + for(int i=0;i +using namespace std; + +//Method1........... +//Time Complexity is O(n*log(n))......... + +int find_k_element(int *a,int n,int k) +{ + return a[k-1]; +} + +//Method2............ +int find_pivot(int *a,int l,int h) +{ + int k=l-1; + int pivot=a[h]; + for(int i=l;i p) + { + low=p+1; + } + else if(k-1 < p) + { + high=p-1; + } + } + return -1; +} + +int main() +{ + int a[] = { 51, 86, 34, 79, 92, 68, 14, 47, 22, 6}; + sort(a,a+10); + cout << find_k_element(a,5,2) << endl; + for(int i=0;i<10;i++) + { + cout << a[find_pos_k(a,10,i+1)]<<" "; + } + return 0; +} diff --git a/sorting/14.cpp b/sorting/14.cpp new file mode 100644 index 0000000..68aad9e --- /dev/null +++ b/sorting/14.cpp @@ -0,0 +1,23 @@ +//Chocolate Distribution Problem...... + +#include +using namespace std; + +//Time complexity is O(n*log(n))...... + +int difference_chocolate(int *a,int n,int m) +{ + int ans=INT_MAX; + for(int i=0;i<=n-m;i++) + { + ans=min(ans,a[m+i-1]-a[i]); + } + return ans; +} +int main() +{ + int a[]={3,4,1,9,56,7,9,12}; + sort(a,a+8); + cout << difference_chocolate(a,8,5); + return 0; +} diff --git a/sorting/15.cpp b/sorting/15.cpp new file mode 100644 index 0000000..5a163e5 --- /dev/null +++ b/sorting/15.cpp @@ -0,0 +1,60 @@ +//Sort an array with two elements....... +//Segregate negative or positive elements........ + +#include +using namespace std; + +//Naive Method........ + +void sort_two(int *a,int n) +{ + int temp[n],index=0; + + for(int i=0;i=0) + { + temp[index]=a[i]; + index++; + } + } + for(int i=0;i=0); + if(i>=j) + return; + swap(a[i],a[j]); + } +} +int main() +{ + int a[]={15,-3,-2,16,-3,-2,15,16}; + //sort_two(a,8); + sort_2(a,8); + for(int i=0;i<8;i++) + { + cout << a[i] <<" "; + } + return 0; +} diff --git a/sorting/16.cpp b/sorting/16.cpp new file mode 100644 index 0000000..f80282b --- /dev/null +++ b/sorting/16.cpp @@ -0,0 +1,80 @@ +//Sort an array of three types of elements......... + +#include +using namespace std; + +//Naive Method......... +void sort_3(int *a,int n) +{ + int temp[n]; + int index=0; + for(int i=0;i +using namespace std; + +int find_time(int a[],int n,int b[],int m) +{ + int i,ma=1,j; + i=1; + j=0; + int count=1; + while(i < n && j +using namespace std; +struct interval +{ + int start,end; +}; + +void merge_interval(struct interval arr[],int n) +{ + int res=0; + for(int i=1;i=arr[i].start) + { + arr[res].start=min(arr[res].start,arr[i].start); + arr[res].end=max(arr[res].end,arr[i].end); + } + else{ + res++; + arr[res]=arr[i]; + } + } + for(int i=0;i<=res;i++) + cout << arr[i].start <<" " << arr[i].end < +using namespace std; + +void merge(int *a,int start,int mid,int end) +{ + int n1=mid-start+1; + int n2=end-mid; + int temp[n1],temp2[n2]; + for(int i=0;i +using namespace std; + +//Method-1 +//Naive-solution..... +//Time complexity is O(n1*n2)............ +void find_intersect(int *a,int *b,int n1,int n2) +{ + for(int i=0;i0 && a[i]==a[i-1]) + continue; + for(int j=0;j0){ + i++; + continue; + } + if(b[j] > a[i]) + { + i++; + } + else if(a[i]>b[j]) + j++; + else + { + cout << a[i] <<" "; + i++;j++; + } + } +} +int main() +{ + int a[]={3,5,10,10,10,15,15,20}; + int b[]={5,10,10,15,30}; + find_intersect(a,b,8,5); + cout< +using namespace std; + +//Method 1 +//Time complexity is O(m+n)...... +//Auxiliary space is O(m+n)......... +void find_union(int *a,int *b,int m,int n) +{ + int temp[m+n]; + int p=m+n; + for(int i=0;i0) + { + j++; + continue; + } + if(b[i]==b[i-1] && i>0) + { + i++; + continue; + } + if(a[j] > b[i]) + { + cout << b[i]<<" "; + i++; + } + else if(b[i] > a[j]) + { + cout << a[j]<<" "; + j++; + } + else + { + cout << a[j] <<" "; + i++; + j++; + } + + } + while(i < n) + { + if(b[i]!=b[i-1] || i==0) + { + cout << b[i] <<" "; + i++; + + } + } + while( j < m) + { + if(a[j]!=a[j-1] || j==0) + { + cout << a[j] <<" "; + j++; + + } + + } +} + + +int main() +{ + int a[]={3,8,10}; + int b[]={2,8,9,10,15}; + find_union(a,b,3,5); + cout << endl; + find_union_1(a,b,3,5); +} diff --git a/sorting/5.cpp b/sorting/5.cpp new file mode 100644 index 0000000..a69c629 --- /dev/null +++ b/sorting/5.cpp @@ -0,0 +1,95 @@ +//Count Inversions in Array............... +//two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j + +//Method 1 +//Naive Solution... + +#include +using namespace std; + +int count_inversion(int *a,int n) +{ + int count=0; + for(int i=0;i a[j]) + count++; + } + } + return count; +} +//Method2...... +//Time Complexity is O(n*log n)................ +//Aux space is O(n)........... +int merge(int *a,int l,int m,int h) +{ + int n1=m-l+1; + int n2=h-m; + int left[n1]; + int right[n2]; + for(int i=0;i +using namespace std; +//Quick sort +//In this alorithm we find the pivot point then partitioning the array such that all elements are smaller than pivot and all elements are greater than pivot. +//To find the Pivot,we will consider three method. +//1.Naive partition +//2.Lumotu partition +//3.hoor's partition +//Method1 +//Naive partition.......... +//Time Complexity is O(n)....... +//Auxiliary Space is O(n)...... + +void partition(int *a,int l,int h,int p) +{ + int temp[h-l+1]; + int index=0; + for(int i=l;i<=h;i++) + { + if(a[i] <=a[p]) + { + temp[index]=a[i]; + index++; + } + } + for(int i=l;i<=h;i++) + { + if(a[i]>a[p]) + { + temp[index]=a[i]; + index++; + } + } + for(int i=l;i<=h;i++) + { + a[i]=temp[i-l]; + cout << a[i] <<" "; + } +} + +//Method 2........ +//Time Complexity is O(n)... +//Auxiliary space is o(1)........... +//Lomuto Partition....... +// We assumed the pivot element is the last-element................ + +int partition_1(int *a,int l,int h) +{ + int pivot=a[h]; + int j=l-1; + for(int i=l;i pivot); + if(start>=end) + return end; + swap(a[start],a[end]); + } +} + +int main() +{ + int a[]={5,3,8,4,2,7,1,10}; + partition(a,0,6,6); + cout< +using namespace std; +//Using Lomuto-Partitioning + +int lomuto_parition(int *a,int l,int h) +{ + int k=l-1; + int pivot=a[h]; + for(int i=l;i l) + { + int p=lomuto_parition(a,l,h); + quick_sort(a,l,p-1); + quick_sort(a,p+1,h); + } +} +//Using Hoore's-Partitioning +int Hoore_parition(int *a,int l,int h) +{ + int low=l-1; + int high=h+1; + int pivot=a[h]; + while(true) + { + do + { + low++; + }while(a[low] < pivot); + do + { + high--; + }while(a[high]>pivot); + if(low>=high) + return high; + swap(a[low],a[high]); + } +} +void quick_sort_Hoore(int *a,int l,int h) +{ + if(h > l) + { + int p=Hoore_parition(a,l,h); + quick_sort_Hoore(a,l,p); + quick_sort_Hoore(a,p+1,h); + } +} + +void display(int *a,int n) +{ + for(int i=0;i +using namespace std; + + +//Lomuto-partition............ + +int lomuto_parition(int *a,int l,int h) +{ + int k=l-1; + int pivot=a[h]; + for(int i=l;i pivot); + + if(beg>=end) + return end; + swap(a[beg],a[end]); + + } +} + +int random_partion_Hoore(int *a,int l,int h) +{ + int ra=l + rand()%(h-l); + swap(a[l],a[ra]); + return Hoore_parition(a,l,h); +} +void Randomized_Quick_sort_Hoore(int *a,int l,int h) +{ + if(h > l) + { + int p=random_partion_Hoore(a,l,h); + Randomized_Quick_sort_Hoore(a,l,p); + Randomized_Quick_sort_Hoore(a,p+1,h); + } +} +void display(int *a,int n) +{ + for(int i=0;i +using namespace std; + + + +void cycle_sort(int *a,int n) +{ + for(int i=0;i a[j]) + { + pos++; + } + } + swap(item,a[pos]); + while(pos!=i) + { + pos=i; + for(int j=i+1;j a[j]) + { + pos++; + } + } + swap(a[pos],item); + } + } +} +void display(int *a,int n) +{ + for(int i=0;i