package sorting;
import java.util.*;
public class QuickSort {
private static List<Integer> sort(List<Integer> data) {
if (data.size() < 2){
return data;
}
List<Integer> lower = new LinkedList<Integer>();
List<Integer> higher = new LinkedList<Integer>();
int pivot = data.get(0);
for (int i = 1; i < data.size(); i++) {
if (data.get(i) < pivot) {
lower.add(data.get(i));
} else {
higher.add(data.get(i));
}
}
sort(lower);
sort(higher);
lower.add(pivot);
lower.addAll(higher);
return lower;
}
public static void main(String[] args) {
List<Integer> unsortedData = new ArrayList<Integer>();
int size = new Random().nextInt(100);
for (int i = 0; i < size; i++) {
unsortedData.add(new Random().nextInt(65536) - 32768);
}
printSort("Unsorted data :", unsortedData);
printSort("Sorted data :", sort(unsortedData));
}
private static void printSort(String message, List<Integer> data) {
System.out.println(message);
for (int item : data) {
System.out.print(item + " ");
}
System.out.println("\n");
}
}
Sunday, January 21, 2018
Quick Sort
Insertion Sort
package sorting;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class InsertionSort {
private static List<Integer> sort(List<Integer> data) {
List<Integer> sortedData = new LinkedList<Integer>();
outerLoop:
for (int number : data) {
for (int i = 0; i < sortedData.size(); i++) {
if (number < sortedData.get(i)) {
sortedData.add(i, number);
continue outerLoop;
}
}
sortedData.add(sortedData.size(), number);
}
return sortedData;
}
public static void main(String[] args) {
List<Integer> unsortedData = new ArrayList<Integer>();
int size = new Random().nextInt(100);
for (int i = 0; i < size; i++) {
unsortedData.add(new Random().nextInt(65536)-32768);
}
printSort("Unsorted data :", unsortedData);
printSort("Sorted data :", sort(unsortedData));
}
private static void printSort(String message, List<Integer> data) {
System.out.println(message);
for (int item : data) {
System.out.print(item + " ");
}
System.out.println("\n");
}
}
Friday, January 19, 2018
Bubble Sort - java 1.5
package sorting;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class BubbleSort {
private static List<Integer> sort(List<Integer> data) {
boolean swapFlag;
do {
swapFlag = false;
for (int i = 0; i < data.size() - 1; i++) {
if (data.get(i) > data.get(i + 1)) {
int tmp = data.get(i + 1);
data.set(i + 1, data.get(i));
data.set(i, tmp);
swapFlag = true;
}
}
} while (swapFlag);
return data;
}
public static void main(String[] args) {
List<Integer> unsortedData = new ArrayList<Integer>();
int size = new Random().nextInt(100);
for (int i = 0; i < size; i++) {
unsortedData.add(new Random().nextInt(100));
}
printSort("Unsorted data :", unsortedData);
printSort("Sorted data :", sort(unsortedData));
}
private static void printSort(String message, List<Integer> data) {
System.out.println(message);
for (int item : data) {
System.out.print(item + " ");
}
System.out.println("\n");
}
}
Subscribe to:
Posts (Atom)