Yurttas/PL/OOL/Java/F/06/02/00/SortAscending.java
Jump to navigation
Jump to search
1/**
2 * Copyright(C) 1998
3 * All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
4 *
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for EDUCATIONAL purposes
7 * and without fee is hereby granted provided that this
8 * copyright notice appears in all copies.
9 *
10 * @date : January 1, 1998.
11 * @author : Salih Yurttas.
12 */
13
14
15public class SortAscending {
16
17 public static int partition(final int[] list,
18 final int i,
19 final int j) {
20
21 int middle = (i+j)/2; // middle as the pivot
22
23 int pivot = list[middle];
24 list[middle] = list[i];
25 list[i] = pivot;
26 int p = i;
27
28 int temp;
29 for(int k=i+1; k<=j; k++)
30 if(list[k]<pivot) { // elements less than pivot
31 temp = list[++p]; // replaced by list[++p]
32 list[p] = list[k];
33 list[k] = temp;
34 }
35 temp = list[i]; // pivot in list[p]
36 list[i] = list[p];
37 list[p] = temp;
38
39 return p; // return pivot position
40 }
41
42 public static void quick(final int[] list,
43 final int i,
44 final int n) {
45 if(i<n) {
46 int p = partition(list,
47 i,
48 n); // p is the list pivot position
49 quick(list,
50 i,
51 p-1);
52 quick(list,
53 p+1,
54 n);
55 }
56 }
57
58}