For theoretical reference:
http://en.wikipedia.org/wiki/Insertion_sort
Java Program:
public class InsertionSort {
public static void main(String[] args) {
int[] input = { 2, 7, 4, 1, 5, 3};
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);
}
}
}
Wednesday, July 2, 2014
Monday, June 30, 2014
Bubble sort algorithm using array in Java.
For theoretical reference you can see wiki link.
http://en.wikipedia.org/wiki/Bubble_sort
For programmatic reference you can check this.
public class BubbleSort {
public static void main(String[] args) {
int arr[] = { 2, 7, 4, 1, 5, 3};
showAll(bubbleSort(arr));
}
private static int [] bubbleSort(int arr[]){
while(true){
int count = 0;
for (int i = 0; i < arr.length - 1; i++) {
if(arr[i] > arr[i+1]){
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
count ++;
}
}
if(count == 0)
break;
}
return arr;
}
private static void showAll(int[] arr){
for (int i : arr) {
System.out.println("" + i);
}
}
}
http://en.wikipedia.org/wiki/Bubble_sort
For programmatic reference you can check this.
public class BubbleSort {
public static void main(String[] args) {
int arr[] = { 2, 7, 4, 1, 5, 3};
showAll(bubbleSort(arr));
}
private static int [] bubbleSort(int arr[]){
while(true){
int count = 0;
for (int i = 0; i < arr.length - 1; i++) {
if(arr[i] > arr[i+1]){
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
count ++;
}
}
if(count == 0)
break;
}
return arr;
}
private static void showAll(int[] arr){
for (int i : arr) {
System.out.println("" + i);
}
}
}
Selection Sort in Java
For theoretical reference you can see wiki link.
http://en.wikipedia.org/wiki/Selection_sort
For programmatic reference you can check this.
public class SelectionSort {
public static void main(String[] args) {
int [] arr = {3, 4, 2, 1, 5, 10, 99, 12, 1, 7};
showAll(descSort(arr));
}
private static int [] descSort(int[] arr){
for(int i = 0; i <= arr.length -1; i++){
for(int j = i+1; j < (arr.length); j++){
if(arr[i] > arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
private static void showAll(int[] arr){
for (int i : arr) {
System.out.println("" + i);
}
}
}
http://en.wikipedia.org/wiki/Selection_sort
For programmatic reference you can check this.
public class SelectionSort {
public static void main(String[] args) {
int [] arr = {3, 4, 2, 1, 5, 10, 99, 12, 1, 7};
showAll(descSort(arr));
}
private static int [] descSort(int[] arr){
for(int i = 0; i <= arr.length -1; i++){
for(int j = i+1; j < (arr.length); j++){
if(arr[i] > arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
private static void showAll(int[] arr){
for (int i : arr) {
System.out.println("" + i);
}
}
}
Subscribe to:
Posts (Atom)