Created
February 11, 2019 12:47
-
-
Save ilhamarrouf/58b1b6cf5a3139b9eefe3285408f73c7 to your computer and use it in GitHub Desktop.
Sorting Java Tutorial
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
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Random; | |
import java.util.Scanner; | |
/** | |
* | |
* @author ilhamarrouf | |
*/ | |
public class LearnSorting { | |
static InputStreamReader inputStreamReader = new InputStreamReader(System.in); | |
static BufferedReader input = new BufferedReader(inputStreamReader); | |
static int[] arr = new int[5]; | |
static boolean isRunning = true; | |
public static void main(String[] args) throws IOException { | |
do { | |
ArrayList al = new ArrayList(); | |
System.out.println("Selamat Datang di Program Simulasi \nMenu"); | |
System.out.println("1. Random Data"); | |
System.out.println("2. Simulasi Bubble Sort - Ascending"); | |
System.out.println("3. Simulasi Selecion Sort – Ascending"); | |
System.out.println("4. Simulasi Bubble Sort – Descending"); | |
System.out.println("5. Simulasi Selection Sort – Descending"); | |
System.out.println("6. Keluar"); | |
System.out.print("Masukkan pilihan anda : "); | |
int selectedMenu = Integer.valueOf(input.readLine()); | |
switch (selectedMenu) { | |
case 1: | |
int botomLimit, topLimit, range, resultRandom; | |
Scanner input = new Scanner(System.in); | |
System.out.print("Batas Bawah : "); | |
botomLimit = input.nextInt(); | |
System.out.print("Batas Atas : "); | |
topLimit = input.nextInt(); | |
Random random = new Random(); | |
for (int counter = 0; counter < 5; counter++) { | |
range = topLimit - botomLimit + 1; | |
resultRandom = random.nextInt(range); | |
arr[counter] = resultRandom; | |
} | |
System.out.print(Arrays.toString(arr)); | |
System.out.println(" "); | |
break; | |
case 2: | |
System.out.println("\nProses Bubble Sort secara Ascending:"); | |
for (int a = 0; a < 4; a++) { | |
System.out.println("Pass " + (a + 1)); | |
for (int b = 0; b < arr.length - 1; b++) { | |
if (arr[b] > arr[b + 1]) { | |
int temp = arr[b]; | |
arr[b] = arr[b + 1]; | |
arr[b + 1] = temp; | |
} | |
System.out.println(Arrays.toString(arr)); | |
} | |
System.out.println(); | |
System.out.print("Result of Pass "+ (a+1) + "\n"); | |
printResult(); | |
System.out.print("\n"); | |
} | |
System.out.println("Hasil Akhir Bubble Sort Ascending: " + Arrays.toString(arr) + "\n"); | |
break; | |
case 3: | |
System.out.println("\nProses Selection Sort secara Ascending:"); | |
int patch; | |
for (int i = 0; i < arr.length - 1; i++) { | |
int index = i; | |
System.out.println("Pass " + (i + 1)); | |
for (int j = i + 1; j < arr.length; j++) { | |
if (arr[j] < arr[index]) { | |
index = j; | |
} | |
if (index != i) { | |
patch = arr[i]; | |
arr[i] = arr[index]; | |
arr[index] = patch; | |
} | |
System.out.println(Arrays.toString(arr)); | |
} | |
System.out.println(); | |
System.out.print("Result of Pass "+ (i+1) + "\n"); | |
printResult(); | |
System.out.print("\n"); | |
} | |
System.out.println("Hasil Akhir Selection Sort Ascending: " + Arrays.toString(arr) + "\n"); | |
break; | |
case 4: | |
System.out.println("\nProses Bubble Sort secara Descending:"); | |
for (int a = 0; a < 4; a++) { | |
System.out.println("Pass " + (a + 1)); | |
for (int b = 0; b < arr.length - 1; b++) { | |
if (arr[b] < arr[b + 1]) { | |
int temp = arr[b]; | |
arr[b] = arr[b + 1]; | |
arr[b + 1] = temp; | |
} | |
System.out.println(Arrays.toString(arr)); | |
} | |
System.out.println(); | |
System.out.print("Result of Pass "+ (a+1) + "\n"); | |
printResult(); | |
System.out.print("\n"); | |
} | |
System.out.println("Hasil Akhir Bubble Sort Descending: " + Arrays.toString(arr) + "\n"); | |
break; | |
case 5: | |
System.out.println("\nProses Selection Sort secara Descending:"); | |
for (int i = 0; i < arr.length - 1; i++) { | |
int index = i; | |
System.out.println("Pass " + (i + 1)); | |
for (int j = i + 1; j < arr.length; j++) { | |
if (arr[j] > arr[index]) { | |
index = j; | |
} | |
if (index != i) { | |
patch = arr[i]; | |
arr[i] = arr[index]; | |
arr[index] = patch; | |
} | |
System.out.println(Arrays.toString(arr)); | |
} | |
System.out.println(); | |
System.out.print("Result of Pass "+ (i+1) + "\n"); | |
printResult(); | |
System.out.print("\n"); | |
} | |
System.out.println("Hasil Akhir Selection Sort Descending: " + Arrays.toString(arr) + "\n"); | |
break; | |
case 6: | |
System.exit(0); | |
break; | |
default: | |
System.out.println("Pilihan salah!"); | |
} | |
} while (isRunning); | |
} | |
static void printResult(){ | |
for (int i=0;i< arr.length ;i++){ | |
System.out.print(arr[i]+ " "); | |
} | |
System.out.print("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment