Created
May 11, 2017 04:47
-
-
Save stmtk1/b9fc4a0786745b92698734e802aea123 to your computer and use it in GitHub Desktop.
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
import java.util.Random; | |
class Sort{ | |
public static void main(String[] args){ | |
Sort sort = new Sort(); | |
int[] arr = sort.makeArray(100); | |
sort.printArray(arr); | |
arr = sort.selection_sort(arr); | |
sort.printArray(arr); | |
} | |
int[] insertion_sort(int[] arr){ | |
for(int i = 1; i < arr.length; i++){ | |
for(int j = i - 1; j > 0; j--){ | |
if(arr[j] < arr[j + 1]) break; | |
int tmp = arr[j]; | |
arr[j] = arr[j + 1]; | |
arr[j + 1] = tmp; | |
} | |
} | |
return arr; | |
} | |
int[] selection_sort(int[] arr){ | |
for(int i = 0; i < arr.length - 1; i++){ | |
int min = i; | |
for(int j = i + 1; j < arr.length; j++){ | |
if(arr[j] < arr[min]) min = j; | |
} | |
int tmp = arr[min]; | |
arr[min] = arr[i]; | |
arr[i] = tmp; | |
} | |
return arr; | |
} | |
int[] makeArray(int num){ | |
Random random = new Random(); | |
int[] ret = new int[num]; | |
for(int i = 0; i < num; i++){ | |
ret[i] = random.nextInt() % 1000; | |
} | |
return ret; | |
} | |
void printArray(int[] arr){ | |
for(int i = 0; i < arr.length; i++){ | |
System.out.print(arr[i]); | |
System.out.print("\t"); | |
} | |
System.out.print("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment