This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Quicksort.java
65 lines (54 loc) · 1.88 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
import java.util.Scanner;
/**
* Java implementation QuickSort.
*/
public class Quicksort {
public static void quicksort(int[] array, int low, int high){
if (low < high) {
int pi = partition(array, low, high);
// Separately sort elements before
// Partition and after partition
quicksort(array, low, pi - 1);
quicksort(array, pi + 1, high);
}
}
private static int partition(int[] array, int low, int high) {
int pivot = array[high];
int i = (low - 1); // Index of smaller element
for (int j= low; j <= high - 1; j++) {
// If current element is smaller than or equal to pivot
if (array[j] <= pivot){
i++; // Increment index of smaller element
int aux = array[i];
array[i] = array[j];
array[j] = aux;
}
}
int aux = array[i + 1];
array[i + 1] = array[high];
array[high] = aux;
return (i + 1);
}
public static void printArray(int[] array, int size) {
for (int i = 0; i < size; i++) {
System.out.print(array[i] + " ");
}
}
public static void main(String args[]){
Scanner inputSize = new Scanner(System.in);
System.out.println("enter number of elements:");
int size = inputSize.nextInt();
Scanner inputElements = new Scanner(System.in);
int[] array = new int[size];
for (int i = 0; i < size; i++) {
System.out.println("enter the element " + (i + 1) + ":");
array[i] = inputElements.nextInt();
}
inputSize.close();
inputElements.close();
Quicksort.quicksort(array, 0, array.length-1);
System.out.println("Sorted array:");
Quicksort.printArray(array, array.length);
System.out.print("\n");
}
}