File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .dev .namhoai .sort ;
2+
3+ public class QuickSort {
4+ public static void sort (int [] a ) {
5+ sort (a , 0 , a .length - 1 );
6+ }
7+
8+ public static void sort (int [] a , int low , int high ) {
9+ if (low >= high ) return ;
10+
11+ int middle = partition (a , low , high );
12+ sort (a , low , middle - 1 );
13+ sort (a , middle + 1 , high );
14+ }
15+
16+ private static int partition (int [] a , int low , int high ) {
17+ int middle = low + (high - low ) / 2 ;
18+ swap (a , middle , high );
19+ int storeIndex = low ;
20+ for (int i = low ; i < high ; i ++) {
21+ if (a [i ] < a [high ]) {
22+ swap (a , storeIndex , i );
23+ storeIndex ++;
24+ }
25+ }
26+ swap (a , high , storeIndex );
27+ return storeIndex ;
28+ }
29+
30+ private static void swap (int [] a , int i , int j ) {
31+ int temp = a [i ];
32+ a [i ] = a [j ];
33+ a [j ] = temp ;
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments