0% found this document useful (0 votes)
53 views

Program Java Insertion Sort

The document describes a Java program that implements insertion sort to sort an array of integers. The program defines an array with initial unsorted data, then uses a for loop with nested for loops to iterate through the array, insert each element into its sorted position, and print the sorted array after each iteration. It outputs the sorted array after each pass with the iteration number to show the progress of the algorithm.

Uploaded by

sarahjulyandini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Program Java Insertion Sort

The document describes a Java program that implements insertion sort to sort an array of integers. The program defines an array with initial unsorted data, then uses a for loop with nested for loops to iterate through the array, insert each element into its sorted position, and print the sorted array after each iteration. It outputs the sorted array after each pass with the iteration number to show the progress of the algorithm.

Uploaded by

sarahjulyandini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

 Program Java Insertion Sort : (Materi Kuliah Analisis Algoritma – 1 April

2020 & 2 April 2020)

/*@author Ir.Endang Sunandar.MKom * /


1. import java.util.Date;
2. public class InsertionSort {
3. public static int buffer;
4. public static int data[]={64,60,32,99,28,16,9,11,35,12,14,1,15,7};
5. public static void main(String args[]){
6. System.out.println("Metode INSERTION SORT");
7. System.out.println("Data awal : 64,60,32,99,28,16,9,11,35,12,14,1,15,7");
8. System.out.println("--------------------------------------------------");
9. Date date = new Date();
10. System.out.println("Tanggal skrg : "+date.toString());
11. System.out.println("----------------------------------------------------------------------");
12. for (int i=1; i<14 ; i++){
13. buffer=data[i];
14. for (int j =i-1;j>=0; j--){
15. int geser=j;
16. if (buffer <data[j]){
17. data[j+1]=data[j];
18. data[j]=buffer; }
19. } // akhir looping j
20. for (int k=0;k<=13;k++){
21. System.out.print(data[k]+", ");
22. } System.out.println(" <------ Urutan data ke : "+(i));
23. } // akhir looping i
24. System.out.println("----------------------------------------------------------------------");
25. System.out.println("Tanggal skrg : "+date.toString());
26. System.out.println("----------------------------------------------------------------------");
27. }
28. }

 Hasil output metode Insertion Sort adalah berikut ini :


Metode INSERTION SORT
Data awal : 64,60,32,99,28,16,9,11,35,12,14,1,15,7

Tanggal skrg : Sat Jul 06 09:07:36 ICT 2019


----------------------------------------------------------------------
60, 64, 32, 99, 28, 16, 9, 11, 35, 12, 14, 1, 15, 7, <------ Urutan data ke : 1
32, 60, 64, 99, 28, 16, 9, 11, 35, 12, 14, 1, 15, 7, <------ Urutan data ke : 2
32, 60, 64, 99, 28, 16, 9, 11, 35, 12, 14, 1, 15, 7, <------ Urutan data ke : 3
28, 32, 60, 64, 99, 16, 9, 11, 35, 12, 14, 1, 15, 7, <------ Urutan data ke : 4
16, 28, 32, 60, 64, 99, 9, 11, 35, 12, 14, 1, 15, 7, <------ Urutan data ke : 5
9, 16, 28, 32, 60, 64, 99, 11, 35, 12, 14, 1, 15, 7, <------ Urutan data ke : 6
9, 11, 16, 28, 32, 60, 64, 99, 35, 12, 14, 1, 15, 7, <------ Urutan data ke : 7
9, 11, 16, 28, 32, 35, 60, 64, 99, 12, 14, 1, 15, 7, <------ Urutan data ke : 8
9, 11, 12, 16, 28, 32, 35, 60, 64, 99, 14, 1, 15, 7, <------ Urutan data ke : 9
9, 11, 12, 14, 16, 28, 32, 35, 60, 64, 99, 1, 15, 7, <------ Urutan data ke : 10
1, 9, 11, 12, 14, 16, 28, 32, 35, 60, 64, 99, 15, 7, <------ Urutan data ke : 11
1, 9, 11, 12, 14, 15, 16, 28, 32, 35, 60, 64, 99, 7, <------ Urutan data ke : 12
1, 7, 9, 11, 12, 14, 15, 16, 28, 32, 35, 60, 64, 99, <------ Urutan data ke : 13
----------------------------------------------------------------------
Tanggal skrg : Sat Jul 06 09:07:36 ICT 2019
----------------------------------------------------------------------

You might also like