Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

No Kegiatan Dan Latihan Script Bubble Sorting

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Kegiatan

No Script
dan Latihan
Bubble public class BubbleSort {
void bubbleSort(int arr[]) {
Sorting
System.out.println("Bubble Sorting");
System.out.print("Data Sebelum di Sorting : ");
for(int x = 0; x < 5; x++)
System.out.print(arr[x]+" ");
int n = arr.length;
for (int i = 0; i < n-1; i++)
Buatlah for (int j = 0; j < n-i-1; j++)
Proses if (arr[j] > arr[j+1]) {
sorting dan int temp = arr[j];
jumlah data arr[j] = arr[j+1];
arr[j+1] = temp;
sesuai }
dengan }
akhiran
1. void printArray(int arr[]) {
NIM
int n = arr.length;
for (int i=0; i<n; ++i)
Catatan: System.out.print(arr[i] + " ");
nilai yang System.out.println();
diinputkan }
random dan
public static void main(String args[]) {
akhiran nim BubbleSort ob = new BubbleSort();
0,1,2,3,4 di int arr[] = {64, 34, 15, 12, 22};
tambah 10 ob.bubbleSort(arr);
System.out.println("");
System.out.print("Data Setelah di Sorting : ");
ob.printArray(arr);
}
}
2. Quick public class QuickSort {
public static void quickSort(int[] input) {
Sorting
int n=input.length;
qsort(input,0,n-1);
}

public static void qsort(int input[],int s,int e) {

if(s>=e)
return;

int a=input[s];

int count=s;
for(int i=s+1;i<=e;i++) {
if(input[i]<=a)
count++;
}

input[s]=input[count];
input[count]=a;

int i=s;
int j=e;
while(i<=count && j>=count) {
if(input[i]<=a)
i++;
else {
if(input[j]<=a) {
int temp=input[j];
input[j]=input[i];
input[i]=temp;
i++;
j--;
}
else
j--;
}
}
qsort(input,s,count-1);
qsort(input,count+1,e);
}

static void printArray(int arr[]) {


3. Shell public class ShellSort {
int[] values;
Sorting
public void sort(){
for(int gap= values.length/2; gap > 0 ; gap = (gap == 2
? 1: (int)(gap/2.2))){
for(int i = gap ; i < values.length; i++){
int j = i;
int cmpItem = values[j];
for( ; j >= gap && cmpItem < values[j-gap]; j
-= gap){
values[j] = values[j-gap];
}
values[j] = cmpItem;
}
}
}

public void print(){


for (int i=0; i < values.length; i++){
System.out.print(values[i]+" ");
}
System.out.println();
}

public void swap(int src, int dst){


int tmp = values[src];
values[src] = values[dst];
values[dst] = tmp;
}

public static void main(String[] args) {


ShellSort ss{= new ShellSort();
class SelectionSort
Selection
void sort(int arr[]) {
Sorting int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

static void printArray(int arr[]) {


int n = arr.length;
for (int i = 0; i < n; ++i)
4. System.out.print(arr[i] + " ");

System.out.println();
}

public static void main(String args[]) {


int arr[] = { 12, 11, 13, 5, 6 };
InsertionSort ob = new InsertionSort();
System.out.println("Selection Sorting");
System.out.print("Data Sebelum di Sorting : ");
for(int x = 0; x < 5; x++)
System.out.print(arr[x]+" ");
ob.sort(arr);
System.out.println("");
System.out.print("Data Setelah di Sorting : ");
printArray(arr);
}
}
5. Insertion class InsertionSort {
void sort(int arr[]) {
Sorting int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

static void printArray(int arr[]) {


int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

public static void main(String args[]) {


6. Merge class MergeSort {
void merge(int arr[], int left, int mid, int right) {
Sorting
int n1 = mid - left + 1;
int n2 = right - mid;

int Left[] = new int[n1];


int Right[] = new int[n2];

for (int i = 0; i < n1; ++i)


Left[i] = arr[left + i];
for (int j = 0; j < n2; ++j)
Right[j] = arr[mid + 1 + j];

int i = 0, j = 0;

int k = left;
while (i < n1 && j < n2) {
if (Left[i] <= Right[j]) {
arr[k] = Left[i];
i++;
}
else {
arr[k] = Right[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = Left[i];
i++;
k++;
}

while (j < n2) {


arr[k] = Right[j];
j++;
k++;
}
}

void sort(int arr[], int left, int right) {


if (left < right) {
int mid = (left + right) / 2;

sort(arr, left, mid);


sort(arr, mid + 1, right);

merge(arr, left, mid, right);


}
}

static void printArray(int arr[]) {


int n = arr.length;
7. Heap public class HeapSort {
public void sort(int arr[]) {
Sorting int n = arr.length;

for (int i = n / 2 - 1; i >= 0; i--)


heapify(arr, n, i);

for (int i=n-1; i>=0; i--) {


int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}

void heapify(int arr[], int n, int i) {


int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;

if (l < n && arr[l] > arr[largest])


largest = l;

if (r < n && arr[r] > arr[largest])


largest = r;

if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;

heapify(arr, n, largest);
}
}

static void printArray(int arr[]) {


int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
8. Bucket public class BucketSort {
public void bucketSort(int[] array){
Sorting
int n = maxVal(array);
int[] bucket = new int[n + 1];

for (int x = 0; x < bucket.length; x++) {


bucket[x] = 0;
}

for (int y = 0; y < array.length; y++) {


bucket[array[y]]++;
}

int outPos = 0;
for (int p = 0; p < bucket.length; p++) {
for (int q = 0; q < bucket[p]; q++) {
array[outPos++] = p;
}
}
}

private int maxVal(int[] array) {


int max = 0;
for (int i : array) {
if (i > max) max = i;
}

return max;
}

public static void main(String[] args){


BucketSort bs = new BucketSort();
int arr[] = {4, 5, 6, 2, 1};
System.out.println("Bucket Sorting");
System.out.print("Data Sebelum di Sorting : ");
for(int x = 0; x < 5; x++)
System.out.print(arr[x]+" ");
9. Radix import java.util.*;
Sorting
class RadixSort {
static int getMax(int arr[], int n) {
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}

static void countSort(int arr[], int n, int exp) {


int output[] = new int[n];
int i;
int count[] = new int[10];
Arrays.fill(count,0);
for (i = 0; i < n; i++)
count[ (arr[i]/exp)%10 ]++;
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--) {
output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
count[ (arr[i]/exp)%10 ]--;
}

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


arr[i] = output[i];
}

static void radixsort(int arr[], int n) {


int m = getMax(arr, n);

for (int exp = 1; m/exp > 0; exp *= 10)


countSort(arr, n, exp);
}

static void print(int arr[], int n) {


for (int i=0; i<n; i++)
System.out.print(arr[i]+" ");
}

public static void main (String[] args) {


int arr[] = {170, 45, 75, 90, 802};
System.out.println("Radix Sorting");
System.out.print("Data Sebelum di Sorting : ");
for(int x = 0; x < 5; x++)
System.out.print(arr[x]+" ");
int n = arr.length;
radixsort(arr, n);

You might also like