Skip to content

Commit

Permalink
quick_sort.cpp Adds QuickSort Algorithm
Browse files Browse the repository at this point in the history
Corrected erros from 1st commit.

Closes NITSkmOS#250
  • Loading branch information
joao-p-marques committed Oct 4, 2018
2 parents 48ed23a + f298c76 commit 305ef02
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Gitter](https://badges.gitter.im/NITSkmOS/algo.svg)](https://gitter.im/NITSkmOS/algo?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![NITSkmOS Algorithms](https://img.shields.io/badge/NITSkmOS-Algorithms-blue.svg)](https://github.com/NITSkmOS/Algorithms)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/29024673e94b48459809432cdd280e39)](https://app.codacy.com/app/NITSkmOS/Algorithms?utm_source=github.com&utm_medium=referral&utm_content=NITSkmOS/Algorithms&utm_campaign=Badge_Grade_Settings)
[![Build Status](https://travis-ci.com/NITSkmOS/Algorithms.svg?branch=master)](https://travis-ci.com/NITSkmOS/Algorithms)
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/NITSkmOS/Algorithms/blob/master/LICENSE)


Expand Down
12 changes: 5 additions & 7 deletions bubble_sort/Cpp/bubble_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <iostream>
using namespace std;

void swap(int *xp, int *yp) {
int temp = *xp;
Expand All @@ -17,8 +18,6 @@ void bubble_sort(int arr[], int n) {
swapped = true;
}
}

// If no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
Expand All @@ -28,17 +27,16 @@ void bubble_sort(int arr[], int n) {
void print_array(int arr[], int size) {
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);

printf("\n");
cout << arr[i] << " ";
cout << endl;
}

// Driver program to test above functions
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: \n");
cout << "Sorted array: " << endl;
print_array(arr, n);
return 0;
}
23 changes: 10 additions & 13 deletions quicksort/Cpp/quick_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@

#include <iostream>

using std::cout;

void quick_sort(int a[], int left, int right) {
int i = left, j = right;
int pivot = a[right];

int i = left, j = right;
int pivot = a[right];

while (i <= j) {
while (i <= j) {
while (a[i] < pivot)
i++; //carry on, this part already sorted
while (a[j] > pivot)
j--; //carry on, this part already sorted
if (i <= j) { // a[i]>=pivot and a[j]<=pivot
//swap a[i] with a[j]
//swap a[i] with a[j]
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
i++;
j--;
}
};
}

if (left < j)
if (left < j)
quick_sort(a, left, j);
if (i < right)
if (i < right)
quick_sort(a, i, right);

}

int main(void){
int main(void) {
int a[10] = { 10, 7, 9, 4, 6, 2, 5, 6, 84, 12};
int a_size = sizeof(a)/sizeof(a[0]);

quick_sort(a, 0, a_size-1);

for(int i=0; i<a_size; i++){
for (int i = 0; i < a_size; i++) {
cout << a[i] << " ";
}
}
}

0 comments on commit 305ef02

Please sign in to comment.