-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathQuickSort.java
68 lines (59 loc) · 1.76 KB
/
QuickSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @date 21/5/21
* @author shaybrow
*/
import java.util.*;
import static org.junit.Assert.*;
import org.junit.Test;
public class QuickSort {
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<arr.length;i++){
System.out.println("Enter next int");
arr[i]=sc.nextInt();
}
quickSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void quickSort (int [] input){
int left = 0;
int right = input.length-1;
if (left < right){
int position =partition(input, left, right);
quickSort(input, left, position-1);
quickSort(input, position+1, right);
}
}
private static int partition(int[] input, int left, int right){
int pivot = input[right];
int low = left -1;
for (int i = left; i <right ; i++) {
if (input[i] <= pivot){
low ++;
swap(input, i, low);
}
}
swap(input, right, low+1);
return low + 1;
}
private static void swap (int [] input, int i, int low){
int temp = input[i];
input[i] = input[low];
input[low] = temp;
}
public void testQuickSort (){
int [] test = {8,4,23,42,16,15};
quickSort(test);
int [] expect = {4,8,15,16,23,16};
assertArrayEquals(expect, test);
int [] test2 = {100, 75, 50, 25, 0};
quickSort(test2);
int [] expect2 = {0,25,50,75,100};
assertArrayEquals(expect2, test2);
}
}