Skip to content

Instantly share code, notes, and snippets.

@h2rashee
Last active August 29, 2015 14:09
Show Gist options
  • Save h2rashee/a6c58b150b545f6e5b88 to your computer and use it in GitHub Desktop.
Save h2rashee/a6c58b150b545f6e5b88 to your computer and use it in GitHub Desktop.
Simple sorting algorithms for ascending order
import java.util.*;
class Sort {
// Bubble Sort algorithm
void bubblesort(int[] arr) {
for(int i = arr.length - 1; i >= 0; i--) {
boolean noSwaps = true;
for(int j = 0; j < i; j++) {
if(arr[j] > arr[j+1]) {
noSwaps = false;
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
if(noSwaps) return;
}
}
// Quick Sort algorithm
void quicksort(int[] arr) {
quicksortPartition(arr, 0, arr.length - 1);
}
void quicksortPartition(int[] arr, int start, int end) {
if(end > start) {
int pivot = partition(arr, start, end);
quicksortPartition(arr, start, pivot-1);
quicksortPartition(arr, pivot+1, end);
}
}
int partition(int[] arr, int start, int end) {
// Prevents integer overflow
int mid = start + (end - start)/2;
int pivot = arr[mid];
int j = start;
int temp = arr[end];
arr[end] = pivot;
arr[mid] = temp;
for(int i = start; i < end; i++) {
if(arr[i] < pivot) {
int swapSpace = arr[i];
arr[i] = arr[j];
arr[j] = swapSpace;
j++;
}
}
temp = arr[j];
arr[j] = arr[end];
arr[end] = temp;
return j;
}
// Merge Sort algorithm
ArrayList<Integer> mergesort(ArrayList<Integer> arr) {
if(arr.size() <= 1) return arr;
int mid = arr.size() / 2;
ArrayList<Integer> left = new ArrayList<Integer>();
ArrayList<Integer> right = new ArrayList<Integer>();
for(int i = 0; i < mid; i++) {
left.add(arr.get(i));
}
for(int i = mid; i < arr.size(); i++) {
right.add(arr.get(i));
}
left = mergesort(left);
right = mergesort(right);
return merge(left, right);
}
ArrayList<Integer> merge(ArrayList<Integer> left, ArrayList<Integer> right) {
int size = left.size() + right.size();
ArrayList<Integer> result = new ArrayList<Integer>(size);
int i = 0, j = 0;
for(int k = 0; k < size; k++) {
if(i < left.size() &&
(j == right.size() || left.get(i) < right.get(j))) {
result.add(left.get(i));
i++;
} else {
result.add(right.get(j));
j++;
}
}
return result;
}
}
class Sorting {
// Test Driver
public static void main(String[] args) {
System.out.println("Enter the elements seperated by spaces: ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
StringTokenizer strToken = new StringTokenizer(input);
int count = strToken.countTokens();
System.out.println("Count: " + count);
// Reads in the numbers to the array
ArrayList<Integer> arr = new ArrayList<Integer>(count);
for(int x = 0; x < count; x++){
arr.add(Integer.parseInt((String)strToken.nextElement()));
}
//int[] array = arr.toArray();
// Run sorting algorithm of choice
Sort s = new Sort();
arr = s.mergesort(arr);
// Print it out
printList(arr);
}
static void printList(int[] arr) {
for(int x = 0; x < arr.length; x++){
System.out.print(arr[x] + " ");
}
System.out.println();
}
static void printList(ArrayList<Integer> arr) {
for(int x = 0; x < arr.size(); x++){
System.out.print(arr.get(x) + " ");
}
System.out.println();
}
}
/*
*
* Sample test cases
*
* [ ]
* [ 1 ]
* [ 10 9 8 7 6 5 4 3 2 1 0 ]
* [ 1 2 3 4 5 6 7 8 9 10 ]
* [ 7 1 3 6 5 8 4 0 9 2 10 ]
* [ 3 1 2 ]
*
*/
@ymeny
Copy link

ymeny commented Nov 15, 2014

What about Heapsort?
And something about maybe finding the median of the first middle and last elements of the array and setting that as a pivot for the pivot in qsort.

@h2rashee
Copy link
Author

Will do that just for you soon! @szbokhar suggested getting into non-comparison-based algorithms but didn't want to go off on a complete tangent. May try count-sort.
Do you have a suggested algorithm @yusongmen for finding median of the first, middle and last somewhat trivially?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment