-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
59 lines (54 loc) · 1.72 KB
/
QuickSort.java
File metadata and controls
59 lines (54 loc) · 1.72 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sortAlgorithms;
/**
*
* @author Vladimir Aca
*/
public class QuickSort {
public static void swap(int [] currentArray, int positionA, int positionB){
int temp = currentArray[positionA];
currentArray[positionA] = currentArray[positionB];
currentArray[positionB] = temp;
}
public static void sort(int[] currentArray, int beginning, int end){
if(beginning > end){
return;
}
int midPosition = getMidPosition(beginning, end);
int leftSide = beginning;
int rightSide = end;
while(leftSide <= midPosition && midPosition <= rightSide){
while(currentArray[leftSide] < currentArray[midPosition]){
leftSide++;
}
while(currentArray[rightSide] > currentArray[midPosition]){
rightSide--;
}
if(leftSide <= rightSide ){
swap(currentArray, leftSide, rightSide);
print(currentArray);
leftSide++;
rightSide--;
}
}
if(beginning<rightSide){
sort(currentArray, beginning, rightSide);
}
if(end> leftSide){
sort(currentArray, leftSide, end);
}
}
public static int getMidPosition(int low, int high){
return low + ((high - low)/2);
}
public static void print(int[] currentArray){
for(Integer element: currentArray){
System.out.print(element + " ");
}
System.out.println("\n");
}
}