Created
October 1, 2015 15:29
-
-
Save wicksome/0304b68494f8c5ca421a 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
| package kr.opid.sorting; | |
| public class SelectionSort { | |
| public static void main(String[] args) { | |
| int[] array = new int[10]; | |
| array[0] = 33; | |
| array[1] = 11; | |
| array[2] = 99; | |
| array[3] = 1; | |
| array[4] = 22; | |
| array[5] = 88; | |
| array[6] = 55; | |
| array[7] = 44; | |
| array[8] = 66; | |
| array[9] = 77; | |
| System.out.println("before)"); | |
| displayArray(array); | |
| System.out.println(); | |
| System.out.println("after)"); | |
| selectionSort(array); | |
| } | |
| static int[] selectionSort(int[] arr) { | |
| int minIdx; | |
| for (int leftIdx = 0; leftIdx < arr.length - 1; leftIdx++) { | |
| minIdx = leftIdx; | |
| for (int idx = leftIdx + 1; idx < arr.length; idx++) { | |
| if (arr[idx] < arr[minIdx]) { | |
| minIdx = idx; | |
| } | |
| } | |
| swap(arr, leftIdx, minIdx); | |
| } | |
| return arr; | |
| } | |
| static void swap(int[] arr, int i, int j) { | |
| int temp = arr[i]; | |
| arr[i] = arr[j]; | |
| arr[j] = temp; | |
| } | |
| static void displayArray(int[] arr) { | |
| for (int i = 0; i < arr.length; i++) { | |
| System.out.print(arr[i] + " "); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment