quickSort

프로그래밍 2008/03/27 20:37

import java.io.*;

public class Round02_Ex01 {

 public static void swap(int[] num, int lhs, int rhs) {
  int tmp = num[lhs];
  num[lhs] = num[rhs];
  num[rhs] = tmp;
 }

 public static void quickSort(int[] num, int l, int h) {

  if (l < h) {
   int m = partion(num, l, h);
   quickSort(num, l, m - 1);
   quickSort(num, m + 1, h);
  }
 }

 public static int partion(int[] num, int l, int h) {

  int firsthigh = l;
  int p = h;

  for (int i = 0; i < h; i++) {
   if (num[i] < num[p]) {
    swap(num, firsthigh, i);
    firsthigh++;

   }
  }
  swap(num, firsthigh, p);

  return firsthigh;
 }

 public static void main(String[] ar) throws IOException {
  int num[] = { 10, 20, 100, 50, 70 };

  quickSort(num, 0, num.length - 1);

  for (int i = 0; i < num.length; i++) {
   System.out.println(num[i]);
  }
 }
}

이올린에 북마크하기(0) 이올린에 추천하기(0)
top

Trackback Address :: http://misterlinker.tistory.com/trackback/21

Write a comment


◀ PREV : [1] : [2] : [3] : [4] : [5] : ... [19] : NEXT ▶