Created
March 1, 2018 22:06
-
-
Save yukeehan/7991243f90b4d3ab43f09d746ae76b6d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TryThis6_3Quicksort { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
char a[] = {'d','x','a','r','p','j','i'}; | |
int i; | |
System.out.println("Original array: "); | |
for(i=0; i<a.length; i++) { | |
System.out.print(a[i]); | |
} | |
System.out.println(); | |
Quicksort.qsort(a); | |
System.out.println("Sorted array: "); | |
for(i=0; i<a.length; i++) { | |
System.out.print(a[i]); | |
} | |
} | |
} | |
class Quicksort{ | |
static void qsort(char items[]) { | |
qs(items, 0, items.length-1); | |
} | |
private static void qs(char items[], int left, int right) { | |
int i,j; | |
char x,y; | |
i=left; j=right; | |
x = items[(left+right)/2]; | |
do { | |
while((items[i] < x) && (i< right)) i++; | |
while((x < items[j]) && (j > left)) j--; | |
if(i<=j) { | |
y = items[i]; | |
items[i] = items[j]; | |
items[j] = y; | |
i++; j--; | |
} | |
}while(i <= j); | |
if(left < j) qs(items, left, j); | |
if(i < right) qs(items, i, right); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment