Skip to content
This repository has been archived by the owner on Feb 21, 2021. It is now read-only.

Commit

Permalink
Restyled by clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
restyled-commits committed Oct 2, 2020
1 parent 2e6c1f4 commit 99d1b79
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 148 deletions.
136 changes: 62 additions & 74 deletions Sorting/bucket_sort/quick_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,104 +3,92 @@ QUICK SORT ALGORITHM : TIME COMPLEXITY O(nlogn(n))
*/


#include<iostream>
#include <iostream>
using namespace std;

//note: call by address in swap function
void swap(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
// note: call by address in swap function
void swap(int *a, int *b) {
int t;
t = *a;
*a = *b;
*b = t;
}

// partition function which makes partition in array and return the partition
// index
int partition(int a[], int low, int high) {
// i is leftmost index and j is the rightmost index
int i = low, j = high;
int pivot = a[low];

//partition function which makes partition in array and return the partition index
int partition(int a[],int low,int high)
{
// i is leftmost index and j is the rightmost index
int i = low,j = high;
int pivot = a[low];


do {
do {
do
{
i++;
} while(a[i]<=pivot); //note :condition is for continuation of loop ans not for stop

do
{
j--;
} while(a[j]>pivot); // note: condition is for continuation of loop and not for stop
i++;
} while (
a[i] <=
pivot); // note :condition is for continuation of loop ans not for stop

do {
j--;
} while (
a[j] >
pivot); // note: condition is for continuation of loop and not for stop

if(i<j) // note: condition
swap(&a[i],&a[j]); // note:
if (i < j) // note: condition
swap(&a[i], &a[j]); // note:

} while(i<j); //note:condition
} while (i < j); // note:condition

swap(&a[low],&a[j]); //note:
swap(&a[low], &a[j]); // note:

return j;
return j;
}

// Quick sort function
void Quick_sort(int a[], int low, int high) {
int j; // j is partition index

//Quick sort function
void Quick_sort(int a[],int low,int high)
{
int j; // j is partition index

//note: This condition for says that min two elements are required for quick sort
if(low < high)
{
//index j is partition index
j = partition(a,low,high) ;

// left partition // note: low to j is for left partition
Quick_sort(a,low,j);
// note: This condition for says that min two elements are required for quick
// sort
if (low < high) {
// index j is partition index
j = partition(a, low, high);

//right partition //note: j+1 to high is for right partition
Quick_sort(a,j+1,high);
// left partition // note: low to j is for left partition
Quick_sort(a, low, j);

}
// right partition //note: j+1 to high is for right partition
Quick_sort(a, j + 1, high);
}
}


//main function
// main function
int main() {

int n;

//Enter the number of elements in array
cin>>n;
int n;

int arr[n];
// Enter the number of elements in array
cin >> n;

//input the custom array
for(int i=0; i<n; i++) {
cin>>arr[i];
}
int arr[n];

// input the custom array
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

cout<<"Before Quick Sort array is : ";
for(int i=0; i<n; i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
cout << "Before Quick Sort array is : ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;

//Quick sort
Quick_sort(arr,0,n);
// Quick sort
Quick_sort(arr, 0, n);

cout<<"After Quick Sort array is : ";
for(int i=0; i<n; i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
cout << "After Quick Sort array is : ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}




136 changes: 62 additions & 74 deletions Sorting/quick_sort/quick_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,104 +3,92 @@ QUICK SORT ALGORITHM : TIME COMPLEXITY O(nlogn(n))
*/


#include<iostream>
#include <iostream>
using namespace std;

//note: call by address in swap function
void swap(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
// note: call by address in swap function
void swap(int *a, int *b) {
int t;
t = *a;
*a = *b;
*b = t;
}

// partition function which makes partition in array and return the partition
// index
int partition(int a[], int low, int high) {
// i is leftmost index and j is the rightmost index
int i = low, j = high;
int pivot = a[low];

//partition function which makes partition in array and return the partition index
int partition(int a[],int low,int high)
{
// i is leftmost index and j is the rightmost index
int i = low,j = high;
int pivot = a[low];


do {
do {
do
{
i++;
} while(a[i]<=pivot); //note :condition is for continuation of loop ans not for stop

do
{
j--;
} while(a[j]>pivot); // note: condition is for continuation of loop and not for stop
i++;
} while (
a[i] <=
pivot); // note :condition is for continuation of loop ans not for stop

do {
j--;
} while (
a[j] >
pivot); // note: condition is for continuation of loop and not for stop

if(i<j) // note: condition
swap(&a[i],&a[j]); // note:
if (i < j) // note: condition
swap(&a[i], &a[j]); // note:

} while(i<j); //note:condition
} while (i < j); // note:condition

swap(&a[low],&a[j]); //note:
swap(&a[low], &a[j]); // note:

return j;
return j;
}

// Quick sort function
void Quick_sort(int a[], int low, int high) {
int j; // j is partition index

//Quick sort function
void Quick_sort(int a[],int low,int high)
{
int j; // j is partition index

//note: This condition for says that min two elements are required for quick sort
if(low < high)
{
//index j is partition index
j = partition(a,low,high) ;

// left partition // note: low to j is for left partition
Quick_sort(a,low,j);
// note: This condition for says that min two elements are required for quick
// sort
if (low < high) {
// index j is partition index
j = partition(a, low, high);

//right partition //note: j+1 to high is for right partition
Quick_sort(a,j+1,high);
// left partition // note: low to j is for left partition
Quick_sort(a, low, j);

}
// right partition //note: j+1 to high is for right partition
Quick_sort(a, j + 1, high);
}
}


//main function
// main function
int main() {

int n;

// n - number of inputs
cin>>n;
int n;

int arr[n];
// n - number of inputs
cin >> n;

//input the custom array
for(int i=0; i<n; i++) {
cin>>arr[i];
}
int arr[n];

// input the custom array
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

cout<<"Before Quick Sort array is : ";
for(int i=0; i<n; i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
cout << "Before Quick Sort array is : ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;

//Quick sort
Quick_sort(arr,0,n);
// Quick sort
Quick_sort(arr, 0, n);

cout<<"After Quick Sort array is : ";
for(int i=0; i<n; i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
cout << "After Quick Sort array is : ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}




0 comments on commit 99d1b79

Please sign in to comment.