[go: up one dir, main page]

0% found this document useful (0 votes)
49 views5 pages

BSI Sort Ecaa

This document provides code examples and outputs of three sorting algorithms: bubble sort, selection sort, and insertion sort. Bubble sort compares adjacent elements and swaps them if they are in the wrong order. Selection sort finds the minimum element and swaps it into the correct position. Insertion sort inserts elements into the sorted portion of the array by comparing to adjacent elements. Figures 1-3 show the outputs of running code examples that implement each of these three sorting algorithms.

Uploaded by

Aisy Nay
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)
49 views5 pages

BSI Sort Ecaa

This document provides code examples and outputs of three sorting algorithms: bubble sort, selection sort, and insertion sort. Bubble sort compares adjacent elements and swaps them if they are in the wrong order. Selection sort finds the minimum element and swaps it into the correct position. Insertion sort inserts elements into the sorted portion of the array by comparing to adjacent elements. Figures 1-3 show the outputs of running code examples that implement each of these three sorting algorithms.

Uploaded by

Aisy Nay
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/ 5

KOLEJ VOKASIONAL KUALA SELANGOR

45600 BESTARI JAYA, SELANGOR

AMALI:
BUBBLE SORT, SELECTION SORT
& INSERTION SORT

NAMA : AISYAH NABILA BINTI MD YAZID

(030911-10-0078)

PROGRAM : 2 DVM KPD / SEM 4 2023

KOD/KURSUS : DKA 4213 PROGRAMMING III


Bubble Sort
package com.mycompany.bubblesortexample;

public class BubbleSortExample {

static void bubbleSort(int[] bilmata){

int n = bilmata.length;
int temp = 0;

for(int i=0; i<n; i++){

for(int j=1; j<(n-i); j++){

if(bilmata[j-1]> bilmata[j]){

//swap element
temp = bilmata[j-1];
bilmata[j-1] = bilmata[j];
bilmata[j] = temp;
}
}
}
}
public static void main(String[] args){

int bilmata[] = {3,60,35,2,45,320,5};

System.out.println("Array Before Bubble Sort");


for(int i=0; i<bilmata.length; i++){

System.out.print(bilmata[i] + " ");


}
System.out.println();

bubbleSort(bilmata);//susun element guna bubble sort

System.out.println("Array After Bubble Sort");


for(int i=0; i<bilmata.length; i++){

System.out.print(bilmata[i] + " ");


}
}
}
Rajah 1 : output BubbleSortExample.java
Selection Sort
package com.mycompany.selectionsortexample;

public class SelectionSortExample {

public static void selectionSort(int[] bilmata){

for(int i=0; i<bilmata.length; i++){

int index = i;
for(int j=i+1; j<bilmata.length; j++){

if(bilmata[j]<bilmata[index]){

index = j;//cari index yg rendah


}
}
int smallerNumber = bilmata[index];
bilmata[index] = bilmata[i];
bilmata[i] = smallerNumber;
}
}
public static void main(String a[]){

int[] bilmata = {9,14,3,2,43,11,58,22};


System.out.println("Before Selection Sort");
for(int i:bilmata){

System.out.print(i+ " ");


}
System.out.println();

selectionSort(bilmata);//susun bilmata guna selection sort

System.out.println("After Selection Sort");


for(int i:bilmata){

System.out.print(i+ " ");


}
}
}

Rajah 2 : output SelectionSortExample.java


Insertion Sort
package com.mycompany.insertionsortexample;

public class InsertionSortExample {

public static void insertionSort(int bilmata[]){

int n = bilmata.length;
for(int j=1; j<n; j++){

int key = bilmata[j];


int i = j-1;

while((i>-1)&&(bilmata[i] >key)){

bilmata[i+1] = bilmata[i];
i--;
}
bilmata[i+1]= key;
}
}
public static void main(String a[]){

int[] bilmata = {9,14,3,2,43,11,58,22};


System.out.println("Before Insertion Sort");
for(int i:bilmata){

System.out.print(i+ " ");


}
System.out.println();

insertionSort(bilmata);//susun bilmata guna selection sort

System.out.println("After Insertion Sort");


for(int i:bilmata){
System.out.print(i+ " ");
}
}
}

Rajah 3 : output InsertionSortExample.java

You might also like