목차
이분검색
합병정렬
빠른정렬
합병정렬
빠른정렬
본문내용
ss(int i, int j) {
return i < j ? true : false;
}
static void printArray(int[] array) {
for (int i=0; i
System.out.print(array[i] + " ");
System.out.println();
}
}
결과 수행화면 (입력 전 -> 입력 후)
빠른정렬
import java.util.*;
public class QuickSort {
public static void main(String[] args) {
int arr[] = new int[20];
Random r = new Random();
int i;
for( i = 0; i < arr.length-1; i++){
arr[i] =(int) r.nextInt(50) +1;
}
System.out.println();
dispArr(arr);
System.out.println();
quickSort(arr,0,arr.length-1);
for(i=0; i
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void quickSort(int[] a, int leftmost,int rightmost){
if(rightmost - leftmost <=0)
return;
else{
int pivot = a[rightmost];
int i = leftmost -1;
int j = rightmost;
while(true){
while (a[++i] < pivot);
while (j > leftmost && a[--j] > pivot);
if(i>=j)
break;
else
swap(a,i,j);
}
swap(a,i,rightmost);
quickSort(a,leftmost,i-1);
quickSort(a,leftmost+1,rightmost);
}
}
static void swap(int[] a,int i, int j){
int temp;
temp = a[i];
a[i] = a[j];
a[j]=temp;
}
static void dispArr(int[] arr){
for (int i=0; i
System.out.print(arr[i]+" ");
}
}
}
결과 수행화면 (입력전 윗줄) (입력후 아랫줄)
return i < j ? true : false;
}
static void printArray(int[] array) {
for (int i=0; i
System.out.println();
}
}
결과 수행화면 (입력 전 -> 입력 후)
빠른정렬
import java.util.*;
public class QuickSort {
public static void main(String[] args) {
int arr[] = new int[20];
Random r = new Random();
int i;
for( i = 0; i < arr.length-1; i++){
arr[i] =(int) r.nextInt(50) +1;
}
System.out.println();
dispArr(arr);
System.out.println();
quickSort(arr,0,arr.length-1);
for(i=0; i
}
System.out.println();
}
public static void quickSort(int[] a, int leftmost,int rightmost){
if(rightmost - leftmost <=0)
return;
else{
int pivot = a[rightmost];
int i = leftmost -1;
int j = rightmost;
while(true){
while (a[++i] < pivot);
while (j > leftmost && a[--j] > pivot);
if(i>=j)
break;
else
swap(a,i,j);
}
swap(a,i,rightmost);
quickSort(a,leftmost,i-1);
quickSort(a,leftmost+1,rightmost);
}
}
static void swap(int[] a,int i, int j){
int temp;
temp = a[i];
a[i] = a[j];
a[j]=temp;
}
static void dispArr(int[] arr){
for (int i=0; i
}
}
}
결과 수행화면 (입력전 윗줄) (입력후 아랫줄)
소개글