Sort Array Java
Baca Juga
Bubble Sort
Bubble Sort bekerja dengan cara menukar satu elemen dengan elemen berikutnya yang lebih kecil berulang-ulang dalam suatu iterasi. Iterasi yang dilakukan adalah sebanyak n-1.
Kelebihan:
- Lebih mudah
- Mudah diimplementasikan
- Mudah dipahami
- Hemat memori
Kekurangan:
- Kompleksitas Waktu O(n^2)
- Terlalu banyak iterasi yang dilakukan
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
public class BubbleSort { | |
public static void main(String[] args) { | |
int[] arrayList = {43, 21, 90, 34, 7, 84, 53}; | |
System.out.println(); | |
System.out.println(); | |
System.out.print("Before Sorting: "); | |
for (int i = 0; i < arrayList.length; i++) { | |
System.out.print(arrayList[i] + " "); | |
} | |
System.out.println(); | |
System.out.print("After Sorting: "); | |
bubbleSort(arrayList); | |
for (int i = 0; i < arrayList.length; i++) { | |
System.out.print(arrayList[i] + " "); | |
} | |
System.out.println(); | |
} | |
private static void bubbleSort(int[] arrayList) { | |
int n = arrayList.length; | |
int temp = 0; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < (n - 1); j++) { | |
if(arrayList[j] > arrayList[j+1]){ | |
temp = arrayList[j]; | |
arrayList[j] = arrayList[j+1]; | |
arrayList[j+1] = temp; | |
} | |
} | |
} | |
} | |
} |
Selection Sort
Selection Sort tidak jauh berbeda dengan Bubble Sort, Selection Sort Bekerja dengan cara menukar elemen terkecil dengan indeks pertama di dalam array, setalh itu menambahkan indeksnya satu dan melakukan terus menerus.
Kelebihan:
- Lebih mudah
- Menggunakan memori yang relatif kecil
Kekurangan:
- Kompleksitas waktu O(n^2)
- Hanya berguna untuk mengurutkan array yang kecil
- Terlalu bnayk melakukan iterasi
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
public class SelectionSort{ | |
public static void main(String[] args) { | |
int[] arrayList = {43, 21, 90, 34, 7, 84, 53}; | |
System.out.println(); | |
System.out.println(); | |
System.out.print("Before Sorting: "); | |
for (int i = 0; i < arrayList.length; i++) { | |
System.out.print(arrayList[i] + " "); | |
} | |
System.out.println(); | |
System.out.print("After Sorting: "); | |
selectionSort(arrayList); | |
for (int i = 0; i < arrayList.length; i++) { | |
System.out.print(arrayList[i] + " "); | |
} | |
} | |
private static void selectionSort(int[] arrayList) { | |
int temp = 0; | |
for (int i = 0; i < arrayList.length; i++) { | |
int min = i; | |
for (int j = 0; j < i+1; j++) { | |
if(arrayList[j] > arrayList[min]){ | |
min = j; | |
} | |
temp = arrayList[min]; | |
arrayList[min] = arrayList[i]; | |
arrayList[i] = temp; | |
} | |
} | |
} | |
} |
Insertion Sort
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
public class InsertionSort { | |
public static void main(String[] args) { | |
int[] arrayList = {43, 21, 90, 34, 7, 84, 53}; | |
System.out.println(); | |
System.out.print("Before Insertion: "); | |
for (int i = 0; i < arrayList.length; i++) { | |
System.out.print(arrayList[i] + " "); | |
} | |
System.out.println(); | |
insetionSort(arrayList); | |
System.out.print("After Insertion: "); | |
for (int i = 0; i < arrayList.length; i++) { | |
System.out.print(arrayList[i] + " "); | |
} | |
System.out.println(); | |
} | |
private static void insetionSort(int[] arrayList) { | |
for (int i = 1; i < arrayList.length; ++i) { | |
int key = arrayList[i]; | |
int j = i - 1; | |
while(j >= 0 && arrayList[j] > key){ | |
arrayList[j + 1] = arrayList[j]; | |
j = j - 1; | |
} | |
arrayList[j + 1] = key; | |
} | |
} | |
} |
Belum ada Komentar untuk "Sort Array Java"
Posting Komentar