LAPORAN HASIL PROGRAM JAVA
( SORTING )
1.
ZAINAL ABIDIN 111210371
2.
VIVI FAKIHAH HARUM 111210367
3.
YUSUF PERMADI 111210370
4.
RIZA KHUSNIATIN 111210351
DOSEN
PEMBIMBING :
MIFTAHUS
SHOLIKHIN, S.Kom, M.Kom.
UNIVERSITAS ISLAM LAMONGAN
2013
public class BubbleSort {
public static void bubble_sort(int array[])
{
System.out.println("====================");
System.out.println("===Bubble
Sort===");
System.out.println("====================");
System.out.println("");
System.out.println("Bubble
sort: 4, 9, 8, 6, 5, 1 ");
System.out.println("Hasilnya:");
int[] input = { 4, 9, 8, 6, 5, 1 };
int n = array.length;
int k;
for (int m = n; m >= 0; m--) {
for (int i = 0; i < n - 1; i++)
{
k = i + 1;
if (array[i] > array[k]) {
swapNumbers(i, k, array);
}
}
tampilkanAngka(array);
}
}
private static void swapNumbers(int i, int
j, int[] array) {
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static void tampilkanAngka(int[]
input) {
for (int i = 0; i < input.length;
i++) {
System.out.print(input[i] + ",
");
}
System.out.println("\n");
}
public static void main(String[] args) {
int[] input = { 4, 9, 8, 6, 5, 1 };
bubble_sort(input);
}
}
HASIL BUBLESORT
SOURCE INSERTIONSORT
package insertionsort;
public class InsertionSort {
public static void
main(String[] args) {
System.out.println("====================");
System.out.println("===Insertion Sort===");
System.out.println("====================");
System.out.println("");
System.out.println("Insertion sort:
4, 9, 8, 6, 5, 1 ");
System.out.println("Hasilnya:");
int[] input = {
4, 9, 8, 6, 5, 1 };
insertionSort(input);
}
private static void
printNumbers(int[] input) {
for (int i = 0; i
< input.length; i++) {
System.out.print(input[i]
+ ", ");
}
System.out.println("\n");
}
public static void
insertionSort(int array[]) {
int n =
array.length;
for (int j = 1; j
< n; j++) {
int key =
array[j];
int i = j-1;
while ( (i
> -1) && ( array [i] > key ) ) {
array
[i+1] = array [i];
i--;
}
array[i+1] =
key;
printNumbers(array);
}
}
}
HASIL INSERTIONSORT
SOURCE SELECTSORT
/*
* To change this
template, choose Tools | Templates
* and open the template
in the editor.
*/
package selecsort;
*
* @author StarLight
*/
class Selecsort
{
public static void
main(String args[])
{
int x[]={4,9,8,6,5,1};
for (int i=0;
i<x.length-1; i++)
{
for (int
j=i+1; j<x.length; j++)
{
if (x[i]
> x[j])
{
//...
Exchange elements
int temp = x[i];
x[i]
= x[j];
x[j]
= temp;
}
}
}
System.out.print("Sorted
Array:");
for(int
i=0;i<x.length;i++) {
System.out.print("\t" +x[i]);
}
}
}
HASIL SELECTSORT
0 komentar:
Posting Komentar