Sorting Algorithms:
Binary Sort:
BubbleSort:
package com.algorithm.sorting;
public class BubbleSortSample {
static void bubbleSort(int[] in) {
int temp;
int n = in.length;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (in[j] < in[i]) {
temp = in[i];
in[i] = in[j];
in[j] = temp;
}
}
}
System.out.println(" After sorting ");
for (int i : in) {
System.out.print(" " + i);
}
}
public static void main(String args[]) {
int[] in = { 12, 2, 34, 5, 44, 12, 4, 7 };
for (int i : in) {
System.out.print(" " + i);
}
bubbleSort(in);
}
}
SelectionSort:
package com.algorithm.sorting;
public class SelectionSortSample {
static void doSelectionSort(int[] in) {
int temp;
int min = 0;
for (int i = 0; i < in.length - 1; i++) {
in[min] = in[i];
for (int j = i + 1; j < in.length; j++) {
if (in[j] < in[min]) {
min = j;
}
}
temp = in[i];
in[i] = in[min];
in[min] = temp;
}
}
public static void main(String args[]) {
int[] in = { 12, 2, 34, 5, 44, 12, 4, 7 };
for (int i : in) {
System.out.print(" " + i);
}
doSelectionSort(in);
System.out.println(" After Sorting");
for (int i : in) {
System.out.print(" " + i);
}
}
}
InsertionSort:
MergeSort:
ShellSort:
HeapSort:
Non-Comparison Sort:
Counting SortRadix Sort
Bucket Sort
Topological Sort:
No comments:
Post a Comment