why is in place merge sort not stable? - java

The implementation below is stable as it used <= instead of < at line marked XXX. This also makes it more efficient. Is there any reason to use < and not <= at this line?
/**
class for In place MergeSort
**/
class MergeSortAlgorithm extends SortAlgorithm {
void sort(int a[], int lo0, int hi0) throws Exception {
int lo = lo0;
int hi = hi0;
pause(lo, hi);
if (lo >= hi) {
return;
}
int mid = (lo + hi) / 2;
/*
* Partition the list into two lists and sort them recursively
*/
sort(a, lo, mid);
sort(a, mid + 1, hi);
/*
* Merge the two sorted lists
*/
int end_lo = mid;
int start_hi = mid + 1;
while ((lo <= end_lo) && (start_hi <= hi)) {
pause(lo);
if (stopRequested) {
return;
}
if (a[lo] <= a[start_hi]) { // LINE XXX
lo++;
} else {
/*
* a[lo] >= a[start_hi]
* The next element comes from the second list,
* move the a[start_hi] element into the next
* position and shuffle all the other elements up.
*/
int T = a[start_hi];
for (int k = start_hi - 1; k >= lo; k--) {
a[k+1] = a[k];
pause(lo);
}
a[lo] = T;
lo++;
end_lo++;
start_hi++;
}
}
}
void sort(int a[]) throws Exception {
sort(a, 0, a.length-1);
}
}

Because the <= in your code assures that same-valued elements (in left- and right-half of sorting array) won't be exchanged.
And also, it avoids useless exchanges.
if (a[lo] <= a[start_hi]) {
/* The left value is smaller than or equal to the right one, leave them as is. */
/* Especially, if the values are same, they won't be exchanged. */
lo++;
} else {
/*
* If the value in right-half is greater than that in left-half,
* insert the right one into just before the left one, i.e., they're exchanged.
*/
...
}
Assume that same-valued element (e.g., ‘5’) in both-halves and the operator above is <.
As comments above shows, the right ‘5’ will be inserted before the left ‘5’, in other words, same-valued elements will be exchanged.
This means the sort is not stable.
And also, it's inefficient to exchange same-valued elements.
I guess the cause of inefficiency comes from the algorithm itself.
Your merging stage is implemented using insertion sort (as you know, it's O(n^2)).
You may have to re-implement when you sort huge arrays.

Fastest known in place stable sort:
http://thomas.baudel.name/Visualisation/VisuTri/inplacestablesort.html

Related

What counts as a binary search comparison?

I'm writing a program that determines how many comparisons it takes to run a binary search algorithm for a given number and sorted array. What I don't understand is what counts as a comparison.
// returns the number of comparisons it takes to find key in sorted list, array
public static int binarySearch(int key, int[] array) {
int left = 0;
int mid;
int right = array.length - 1;
int i = 0;
while (true) {
if (left > right) {
mid = -1;
break;
}
else {
mid = (left + right)/2;
if (key < array[mid]) {
i++;
right = mid - 1;
}
else if (key > array[mid]) {
i++;
left = mid + 1;
}
else {
break; // success
}
}
}
return i;
}
The function returns i, which is supposed to be the total number of comparisons made in finding the key in array. But what defines a comparison? Is it any time there is a conditional?
Thanks for any help, just trying to understand this concept.
Usually, a comparison occurs each time the key is compared to an array element. The code seems to not be counting that, though. It is counting how many times one of the search boundaries (left or right) is changed. It's not exactly the same thing being counted, but it's pretty close to the same thing, since the number of times a boundary is shifted is directly related to the number of times through the loop and hence to the number of times a comparison is made. At most, the two ways of counting will be off by 1 or 2 (I didn't bother to figure that out exactly).
Note also that if one were to use the usual definition, the code could be rewritten to use Integer.compare(int,int) do a single comparison of key with array[mid] to determine whether key was less than, equal to, or greater than array[mid].
public static int binarySearch(int key, int[] array) {
int left = 0;
int mid;
int right = array.length - 1;
int i = 0;
while (left <= right) {
mid = (left + right)/2;
int comp = Integer.compare(key, array[mid]);
i++;
if (comp < 0) {
right = mid - 1;
}
else if (comp > 0) {
left = mid + 1;
}
else {
break; // success
}
}
return i;
}

Inplace Quicksort in Java

For refreshing some Java I tried to implement a quicksort (inplace) algorithm that can sort integer arrays. Following is the code I've got so far. You can call it by sort(a,0,a.length-1).
This code obviously fails (gets into an infinite loop) if both 'pointers' i,j point each to an array entry that have the same values as the pivot. The pivot element v is always the right most of the current partition (the one with the greatest index).
But I just cannot figure out how to avoid that, does anyone see a solution?
static void sort(int a[], int left, int right) {
if (right > left){
int i=left, j=right-1, tmp;
int v = a[right]; //pivot
int counter = 0;
do {
while(a[i]<v)i++;
while(j>0 && a[j]>v)j--;
if( i < j){
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
} while(i < j);
tmp = a[right];
a[right] = a[i];
a[i] = tmp;
sort(a,left,i-1);
sort(a,i+1,right);
}
}
When preforming a Quicksort I strongly suggest making a separate method for partitioning to make the code easier to follow (I'll show an example below). On top of this a good way of avoiding worst case run time is shuffling the array you're sorting prior to preforming the quick sort. Also I used the first index as the partitioning item instead of the last.
For example:
public static void sort (int[] a)
{
StdRandom.shuffle(a);
sort(a, 0, a.length - 1);
}
private static void sort(int[] a, int lo, int hi)
{
if (hi <= lo) return;
int j = partition(a, lo, hi) // the addition of a partitioning method
sort(a, lo, j-1);
sort(a, j+1, hi);
}
private static int partition(int[] a, int lo, int hi)
{
int i = lo, j = hi + 1, tmp = 0;
int v = a[lo];
while (true)
{
while (a[i++] < v) if (i == hi) break;
while (v < a[j--]) if (j == lo) break;
if (i >= j) break;
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
tmp = a[lo];
a[lo] = a[j];
a[j] = temp;
return j;
}
On top of this if you want a really good example on how Quicksort works (as a refresher) see here.
This should work (will check for correctness in a bit, it works!):
EDIT: I previously made a mistake in error checking. I forgot to add 2 more conditions, here is the amended code.
public static void main (String[] args) throws java.lang.Exception
{
int b[] = {10, 9, 8, 7, 7, 7, 7, 3, 2, 1};
sort(b,0,b.length-1);
System.out.println(Arrays.toString(b));
}
static void sort(int a[], int left, int right) {
if (right > left){
int i=left, j=right, tmp;
//we want j to be right, not right-1 since that leaves out a number during recursion
int v = a[right]; //pivot
do {
while(a[i]<v)
i++;
while(a[j]>v)
//no need to check for 0, the right condition for recursion is the 2 if statements below.
j--;
if( i <= j){ //your code was i<j
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
i++;
j--;
//we need to +/- both i,j, else it will stick at 0 or be same number
}
} while(i <= j); //your code was i<j, hence infinite loop on 0 case
//you had a swap here, I don't think it's needed.
//this is the 2 conditions we need to avoid infinite loops
// check if left < j, if it isn't, it's already sorted. Done
if(left < j) sort(a,left,j);
//check if i is less than right, if it isn't it's already sorted. Done
// here i is now the 'middle index', the slice for divide and conquer.
if(i < right) sort(a,i,right);
}
}
This Code in the IDEOne online compiler
Basically we make sure that we also swap the value if the value of i/j is the same as the pivot, and break out of the recursion.
Also there was a check in the pseudocode for the length, as if we have an array of just 1 item it's already sorted (we forgot the base case), I thought we needed that but since you pass in the indexes and the entire array, not the subarray, we just increment i and j so the algorithm won't stick at 0 (they're done sorting) but still keep sorting an array of 1. :)
Also, we had to add 2 conditions to check if the array is already sorted for the recursive calls. without it, we'll end up sorting an already sorted array forever, hence another infinite loop. see how I added checks for if left less than j and if i less than right. Also, at that point of passing in i and j, i is effectively the middle index we split for divide and conquer, and j would be the value right before the middle value.
The pseudocode for it is taken from RosettaCode:
function quicksort(array)
if length(array) > 1
pivot := select any element of array
left := first index of array
right := last index of array
while left ≤ right
while array[left] < pivot
left := left + 1
while array[right] > pivot
right := right - 1
if left ≤ right
swap array[left] with array[right]
left := left + 1
right := right - 1
quicksort(array from first index to right)
quicksort(array from left to last index)
Reference: This SO question
Also read this for a quick refresher, it's implemented differently with an oridnary while loop
This was fun :)
Heres some simple code I wrote that doesn't initialize to many pointers and gets the job done in a simple manner.
public int[] quickSort(int[] x ){
quickSortWorker(x,0,x.length-1);
return x;
}
private int[] quickSortWorker(int[] x, int lb, int ub){
if (lb>=ub) return x;
int pivotIndex = lb;
for (int i = lb+1 ; i<=ub; i++){
if (x[i]<=x[pivotIndex]){
swap(x,pivotIndex,i);
swap(x,i,pivotIndex+1);
pivotIndex++;
}
}
quickSortWorker(x,lb,pivotIndex-1);
quickSortWorker(x,pivotIndex+1,ub);
return x;
}
private void swap(int[] x,int a, int b){
int tmp = x[a];
x[a]=x[b];
x[b]=tmp;
}

Why is the merge-sort taking too much time to sort?

I've been trying to optimize this mergesort version but it takes too long to sort around 3 million of registers. Where am I doing it wrong? I would appreciate some help, thanks.
Persona is a class that has a string and an Integer, just in case that you guys want to know in order to help me.
public class Mergesort {
private ArrayList<Persona> numbers = new ArrayList();
private ArrayList<Persona> helper;
private int number;
private boolean ascending;
public void sort(ArrayList<Persona> values, boolean ascending) {
this.numbers = values;
this.ascending = ascending;
number = values.size();
helper = new ArrayList();
mergesort(0, number - 1);
}
/**
* Determines the middle of the array to sort the left side and the right side
* Then it merges both arrays.
* #param low
* #param high
*/
private void mergesort(int low, int high) {
// check if low is smaller then high, if not then the array is sorted
if (low < high) {
// Get the index of the element which is in the middle
int middle = low + (high - low) / 2;
// Sort the left side of the array
mergesort(low, middle);
// Sort the right side of the array
mergesort(middle + 1, high);
// Combine them both
merge(low, middle, high);
}
}
/**
* Merges the arrays.
* #param low
* #param middle
* #param high
*/
private void merge(int low, int middle, int high) {
// Copy both parts into the helper array
for (int i = low; i <= high; i++) {
helper.add(i, numbers.get(i));
}
int i = low;
int j = middle + 1;
int k = low;
// Copy the smallest values from either the left or the right side back
// to the original array
while (i <= middle && j <= high) {
if ( helper.get(i).id <= helper.get(j).id) {
numbers.set(k, helper.get(i));
i++;
} else {
numbers.set(k,helper.get(j));
j++;
}
k++;
}
// Copy the rest of the left side of the array into the target array
while (i <= middle) {
numbers.set(k,helper.get(i));
k++;
i++;
}
}}
You never clear out the contents of helper (which should not be a global anyawy), which means each time you are merging more and more data. I'm really surprised you didn't get out-of-memory.
Is your code running and the o/p is alryt?
In the merge function there must be another loop,after the first while loop. The first while loop terminated because either j>high or i>middle. You just wrote j>high condition,dere is no i>middle condtn.After that loop thre must be something like dis
if(j>high)
{
while (i <= middle) {
numbers.set(k,helper.get(i));
k++;
i++;
}
}
else
{
while (j <= high) {
numbers.set(k,helper.get(j));
k++;
j++;
}
}
N clear the helper

How to find the median of a large number of integers (they dont fit in memory)

I know the answer is using median of medians but can someone explain how to do it?
There are linear time algorithms to do this, this page might be helpful, http://en.wikipedia.org/wiki/Selection_algorithm, if you are still confused just ask
Basically the way the selection algorithm works is like a quicksort but it only sorts on side of the pivot each time. The goal is to keep partitioning until you choose the pivot equal to the index of the element you were trying to find. Here is java code I found for quickselect:
public static int selectKth(int[] arr, int k) {
if (arr == null || arr.length <= k)
throw new Error();
int from = 0, to = arr.length - 1;
// if from == to we reached the kth element
while (from < to) {
int r = from, w = to;
int mid = arr[(r + w) / 2];
// stop if the reader and writer meets
while (r < w) {
if (arr[r] >= mid) { // put the large values at the end
int tmp = arr[w];
arr[w] = arr[r];
arr[r] = tmp;
w--;
} else { // the value is smaller than the pivot, skip
r++;
}
}
// if we stepped up (r++) we need to step one down
if (arr[r] > mid)
r--;
// the r pointer is on the end of the first k elements
if (k <= r) {
to = r;
} else {
from = r + 1;
}
}
return arr[k];
}
here is the Median of Medians algorithm. check this out
See the first two answers to this question. If the first one (frequency counts) can work for your data / available storage, you can get the exact answer that way. The second (remedian) is a robust, general method.

Quicksort - recursive

I am trying to code quicksort in two ways, one in-place, and the other by using separate arrays. I'm kind of stuck on some of the logic, take a look at what I have, Thanks for the help in advance!
public List<Integer> sort(List<Integer> arr){
if(arr.length > 0)
List<Integer> ret = new ArrayList<Integer>();
ret = quickSort(arr);
return ret;
}
public List<Integer> quickSort(List<Integer> arr){
if(arr.length < 2)
return;
int pivot = arr[0];
List<Integer> left = new ArrayList<Integer>();
List<Integer> right = new ArrayList<Integer>();
for(int i = 0; i < arr.length; i++){
if(arr[i] <= pivot)
left.add(arr[i]);
else
right.add(arr[i]);
}
quickSort(left);
quickSort(right);
}
Now i'm stuck, I don't know what I would do after recursively going through both sets, mostly stuck on how would I connect them together and return a sorted list.
You need to combine left and right sequences together. You need to do it at the end of your algorithm (before the closing }). In pseudo code:
int leftpos = 0, rightpos = 0;
List newlist = new ArrayList();
for(int pos = 0; pos < arr.length; pos++)
if left[pos] < right[pos] newlist.add(left[leftpos++]);
else newlist.add(right[rightpos++]);
return newlist;
This is just a pseudo-code. You need to add code to check lengths of each array (left and right) in the for cycle.
Also I must note that this is far from quicksort. So many new array allocations make the algorithm extremely slow and that's unwelcome when sorting.
Also, right side of line 3 is redundant. You don't need to allocate anything here, as it is overwritten in the next line. I would just simply replace your lines 3-5 with this:
return quickSort(arr);
Let me have a crack at this for you.
First off, you always want to do in-place sorting unless you're working with linked lists (and even then it usually pays to convert to an array, sort in place, then convert back to a linked list -- it puts way less pressure on the garbage collector). .NET List<>s are actually expanding arrays.
Next, quicksort is really all about the pivot operation. Here's one way to do it:
// Quicksort the sub-array xs[lo..hi].
void QSort(int[] xs, int lo, int hi) {
if (hi <= lo) return; // Don't sort empty or singleton sub-arrays.
var p = [choose some pivot value from xs[lo..hi]];
var a = lo; // Invariant: x[lo..a - 1] <= p.
var z = hi; // Invariant: p < x[z + 1..hi].
while (a <= z) {
if (xs[a] <= p) a++; else Swap(xs, a, z--);
}
QSort(xs, lo, a - 1); // Sort the items <= p.
QSort(xs, z + 1, hi); // Sort the items > p.
}
void Swap(int[] xs, int i, int j) {
var tmp = xs[i];
xs[i] = xs[j];
xs[j] = tmp;
}
Simple implementation on Groovy
def qs(list) {
if (list.size() < 2) return list
def pivot = list[0]
def items = list.groupBy { it <=> pivot }.withDefault { [] }
qs(items[-1]) + items[0] + qs(items[1])
}

Categories

Resources