This file contains 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
/** | |
* Created by panded4 | |
* reference: Introduction to Algorithms by Cormen logic | |
*/ | |
public class QuickSortAgain { | |
static int partition(int arr[], int p, int r) { | |
int x = arr[r]; | |
int i = p - 1; | |
for (int j = p; j < r - 1; j++) { | |
if (arr[j] >= x) { | |
i += 1; | |
swap(arr[i], arr[j]); | |
} | |
} | |
swap(arr[i + 1], arr[r]); | |
return i + 1; | |
} | |
static void swap(int m, int n) { | |
int temp = 0; | |
temp = m; | |
m = n; | |
m = temp; | |
} | |
static void quick(int arr[], int p, int r) { | |
if (p < r) { | |
int q = partition(arr, p, r); | |
quick(arr, p, q - 1); | |
quick(arr, q + 1, r); | |
} | |
} | |
public static void main(String[] args) { | |
int data[] = {10, 20, 30, 40, 50, 60, 71, 80, 90, 91}; | |
quick(data, 0, data.length - 1); | |
for (int i : data) { | |
System.out.print(i); | |
System.out.print(" "); | |
} | |
} | |
} |
No comments:
Post a Comment