Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 184 additions & 0 deletions DataStructure_and_Algorithms/CPP/Dynamic Programming/quickSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@

// ............................. Quick Sort : O(NlogN) ......................
// principle : it choose a pivote element from array and part
// the array around that element it, continue untill it cannot be part further then sort every part

#include<bits/stdc++.h>
using namespace std;

// partition function
int partition(int arr[], int l, int r){

// 3 approach

// if pivote is first element
//...............................................................................................


/*
int pivote = arr[l];
int i = l;

for(int j=l+1; j<=r; j++){
if(arr[j] < pivote){
i++;
swap(arr[j],arr[i]);
}
}

swap(arr[i],arr[l]);
return i++;

*/


//.....................OR.....................................................................

/*
int pivote = arr[l];
int index=0;
for(int i=l; i<=r; i++)
if(arr[i]<pivote)
index++;

swap(arr[l],arr[l+index]);
int i=l,j=r;
while(i<(l+index) && j>(l+index)){
if(arr[i]<pivote)
i++;
else if(arr[j]>=pivote)
j--;
else{
swap(arr[i],arr[j]);
i++; j--;
}
}

return l+index;

*/








// if pivote is last element
//.................................................................................................

/*
int pivote = arr[r];
int i = l-1;

for(int j=l; j<r; j++){
if(arr[j] < pivote){
i++;
swap(arr[j], arr[i]);
}
}
swap(arr[i+1], arr[r]);
return i++;

*/
//......................OR....................................................................

/*
int pivote = arr[r];
int index=0;
for(int i=l; i<=r; i++)
if(arr[i]<pivote)
index++;

swap(arr[r],arr[l+index]);
int i=l,j=r;
while(i<(l+index) && j>(l+index)){
if(arr[i]<pivote)
i++;
else if(arr[j]>=pivote)
j--;
else{
swap(arr[i],arr[j]);
i++; j--;
}
}

return l+index;

*/







// if middle element is pivote
//.............................................................................................


int mid = l + (r-l)/2;
int pivote = arr[mid];

int index=0;
for(int i=l; i<=r; i++)
if(arr[i]<pivote)
index++;

swap(arr[mid],arr[l+index]);

int i=l,j=r;
while(i<(l+index) && j>(l+index)){
if(arr[i]<pivote)
i++;
else if(arr[j]>=pivote)
j--;
else{
swap(arr[i],arr[j]);
i++; j--;
}
}

return l+index;


}


// Quick Sort Function
void quickSort(int arr[], int l, int r){
if(l>=r)
return;

int pi = partition(arr, l, r);

quickSort(arr,l,pi-1);
quickSort(arr,pi+1,r);
}


int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
freopen("output.txt","w",stdout);

int test; cin >> test;
while(test--){
int n; cin >> n;
int arr[n];

for(int i=0; i<n; i++)
cin >> arr[i];

quickSort(arr,0,n-1);

for(int x : arr)
cout << x << " ";

cout << endl;
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//.............. Selection sort O(N^2) ................
// princple : it is something like insertion sort , it has two array sorted and unsorted
// each time it pick the minimum element from unsorted array and put it in sorted array at right position(at last)


#include<bits/stdc++.h>
using namespace std;


// function for sort by recursion (select max element and put at last i.e first of sorted array)
void selectionSort1(int arr[], int n){
if(n<=1)
return ;

int max = INT_MIN;
int maxIndex = n-1;
for(int i=0; i<n; i++)
if(max < arr[i]){
max = arr[i];
maxIndex = i;
}
if(maxIndex != n-1){
arr[maxIndex] = arr[n-1];
arr[n-1] = max;
}

selectionSort1(arr,n-1);
}
// OR .... (Select Min and put at first i.e. last of sorted array)
void secondRecursion(int arr[], int n, int index=0){
if(n<=index)
return ;

int min = INT_MAX;
int minIndex = index;
for(int i=index; i<n; i++)
if( min > arr[i]){
min = arr[i];
minIndex = i;
}
if(minIndex != index){
arr[minIndex] = arr[index];
arr[index] = min;

}
secondRecursion(arr,n,index+1);
}

// by iterator .....
void selectionSort2(int arr[], int n){
if(n<=1)
return ;

int lastIndexOfSorted = 0;
for(int i=0; i<n-1; i++){
int min = INT_MAX;
int minIndex = i;
for(int j=lastIndexOfSorted; j<n; j++)
if(min > arr[j]){
min = arr[j];
minIndex = j;
}
if(minIndex != lastIndexOfSorted){
arr[minIndex] = arr[lastIndexOfSorted];
arr[lastIndexOfSorted] = min;
}
lastIndexOfSorted++;
}


}


int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
freopen("output.txt","w",stdout);

int test; cin >> test;
while(test--){
int n; cin >> n;
int arr[n];

for(int i=0; i<n; i++)
cin >> arr[i];

selectionSort1(arr,n); // recursion
//secondRecursion(arr,n); // recursion
//selectionSort2(arr,n); // iterartor

for(int x : arr)
cout << x << " ";

cout << endl;
}

return 0;
}
72 changes: 72 additions & 0 deletions DataStructure_and_Algorithms/CPP/Segment Tree/segmentree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include<bits/stdc++.h>
using namespace std;


void updateSegmentTree(int arr[], int tree[], int start, int end, int node, int idx, int value){
if(start==end){
return;
}
if(start==idx || end==idx){
arr[idx] = value;
tree[node] = arr[idx];
return ;
}

int mid = (start+end)/2;
int node1 = node*2;
int node2 = node*2+1;
if(mid<=idx){
updateSegmentTree(arr, tree, start, mid, node1, idx, value);
}else {
updateSegmentTree(arr, tree, mid+1, end, node2, idx, value);
}

tree[node] = tree[node1]+tree[node2];
}

void makeSegmentTree(int arr[], int tree[], int start, int end, int node){
if(start==end){
tree[node] = arr[start];
return ;
}
int mid = (start+end)/2;
int node1 = node*2;
int node2 = node*2+1;
makeSegmentTree(arr, tree, start, mid, node1);
makeSegmentTree(arr, tree, mid+1, end, node2);
tree[node] = tree[node1]+tree[node2];
}

int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif

int n;
cin >> n;

int arr[n] ;
for(int i=0; i<n; i++){
cin >> arr[i];
}

for(int i : arr) cout << i << " ";
cout << endl;
int* tree = new int[2*n];
makeSegmentTree(arr, tree, 0, n-1, 1);
for(int i=1; i<2*n; i++)
cout << tree[i] << " ";

cout << endl;

updateSegmentTree(arr, tree, 0, n-1, 1, 4, 200);
for(int i=1; i<2*n; i++)
cout << tree[i] << " ";

cout << endl;
for(int i : arr) cout << i << " ";

// cout << findSegmentSum(tree, (2*n), 1, 3,6);
return 0;
}
Loading