Skip to content

Commit d455d91

Browse files
committed
quick sort premaseem implementation
1 parent 14236bb commit d455d91

File tree

3 files changed

+66
-55
lines changed

3 files changed

+66
-55
lines changed

src/me/premaseem/algorithm/quicksort/App.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package me.premaseem.algorithm.quicksort;
2+
3+
import me.premaseem.MyUtils;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
public class QuickSort {
8+
// Test Driven Development by Aseem Jain
9+
@Test
10+
public void test() {
11+
int a2[] = {10, 30, 190, 120, 110, 50, 7};
12+
13+
sort(a2, 0, 6);
14+
MyUtils.isArrSorted(a2);
15+
}
16+
17+
18+
19+
int partition(int arr[], int low, int high)
20+
{
21+
int pivot = arr[high];
22+
int i = (low-1); // index of smaller element
23+
for (int j=low; j<high; j++)
24+
{
25+
// If current element is smaller than the pivot
26+
if (arr[j] < pivot)
27+
{
28+
i++;
29+
30+
// swap arr[i] and arr[j]
31+
int temp = arr[i];
32+
arr[i] = arr[j];
33+
arr[j] = temp;
34+
}
35+
}
36+
37+
// swap arr[i+1] and arr[high] (or pivot)
38+
i++;
39+
int temp = arr[i];
40+
arr[i] = arr[high];
41+
arr[high] = temp;
42+
43+
return i;
44+
}
45+
46+
47+
/* The main function that implements QuickSort()
48+
arr[] --> Array to be sorted,
49+
low --> Starting index,
50+
high --> Ending index */
51+
void sort(int arr[], int low, int high)
52+
{
53+
if (low < high)
54+
{
55+
/* pi is partitioning index, arr[pi] is
56+
now at right place */
57+
int pi = partition(arr, low, high);
58+
59+
// Recursively sort elements before
60+
// partition and after partition
61+
sort(arr, low, pi-1);
62+
sort(arr, pi+1, high);
63+
}
64+
}
65+
66+
}

src/me/premaseem/algorithm/quicksort/Test.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)