Counting Comparisons When The Pivot Is Not The First Element - java

I have tried to implement the Quicksort algorithm, but different pivots don't seem to work.
When the pivot element is not the first element, it always ends in a recursive loop resulting in a crash where the variable i falls off of the array and it calls itself again with the full array and nothing changes. I think the error is the comparison comparing toSort[j] with the pivot value, or the first element after being swapped with the other element.
public static void quickSort(int[] toSort, int l, int r){
if(r - l <= 1)return;
counter += r - l - 1;
int p = choosePivot(l, r);
int pivot = toSort[p];
int oldP = toSort[p];
toSort[p] = toSort[l];
toSort[l] = oldP;
int i = l + 1;
for(int j = l + 1; j < r; j++){
if(toSort[j] < pivot){
int swap = toSort[j];
toSort[j] = toSort[i];
toSort[i] = swap;
i++;
}
}
oldP = toSort[i - 1];
toSort[i - 1] = toSort[l];
toSort[l] = oldP;
quickSort(toSort, l, i);
quickSort(toSort, i, r);
}
public static int choosePivot(int m, int n){
return n - 1;
//return m;
}

The problem is that the recursive call to quickSort() is not converging. A slight change in the last two lines will work like magic.
public static void quickSort(int[] toSort, int l, int r){
if(r - l <= 1)return;
counter += r - l - 1;
int p = choosePivot(l, r);
int pivot = toSort[p];
int oldP = toSort[p];
toSort[p] = toSort[l];
toSort[l] = oldP;
int i = l + 1;
for(int j = l + 1; j < r; j++){
if(toSort[j] < pivot){
int swap = toSort[j];
toSort[j] = toSort[i];
toSort[i] = swap;
i++;
}
}
oldP = toSort[i - 1];
toSort[i - 1] = toSort[l];
toSort[l] = oldP;
quickSort(toSort, l, i-1);
quickSort(toSort, i, r);
}
public static int choosePivot(int m, int n){
return n - 1;
//return m;
}
You can check the Ideone link for updated code.

Related

First and last pivot element vs generic placement with very large N

I've implemented a QuickSort algorithm along with a Time-Complexity control. It works fine with smaller N but once i get closer to larger N the StackOverflow is inevitable. Im thinking that having the pivot element as the last element might be what's causing this.
My first thought was to simply always use the middle element as the pivot element to avoid this but since the test-program throws an 'unsorted exception', it's not a valid solution.
Any ideas how i can work my way around this?
public class QuickSorter implements IntSorter{
int partition (int a[], int lo, int hi) {
int pivot = a[hi]; // pivot element
int i = (lo - 1);
for (int j = lo; j <= hi - 1; j++) {
if (a[j] < pivot) {
i++;
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
int temp = a[i+1];
a[i+1] = a[hi];
a[hi] = temp;
return (i + 1);
}
#Override
public void sort(int[] a) {
int lo = 0;
int hi = a.length-1;
if (lo < hi) {
int p = partition(a, lo, hi);
sort(a, lo, p - 1);
sort(a, p + 1, hi);
}
}
private void sort(int[] a, int lo, int hi) {
if (lo < hi) {
int p = partition(a, lo, hi);
sort(a, lo, p - 1);
sort(a, p + 1, hi);
}
}
}
Testcode:
private static void testSort(IntSorter sorter, int firstN, boolean ordered) {
double t1 = 0;
int N = firstN/2;
while (t1 < 0.7 && N < 10000000) {
N *= 2;
int[] a = create(N, ordered);
t1 = timeit(sorter, a);
System.out.println("T("+N+")="+t1);
ArrayUtil.testOrdered(a);
}
int[] a = create(4*N, ordered);
double t4 = timeit(sorter, a);
ArrayUtil.testOrdered(a);
double t01 = t1 / (N * Math.log(N ));
double t04 = t4 / (4*N * Math.log(4*N));
System.out.println("T("+4*N+")="+t4+" growth per N log N: "+t04/t01);
if (t04/t01 > 1.25) {
System.out.println(sorter.getClass().getName()+".sort appears not to run in O(N log N) time");
System.exit(1);
}
}
public static void testOrdered(int[] a) {
int N = a.length;
for (int i = 1; i < N; i++) {
if (a[i] < a[i-1]) {
throw new SortingException("Not sorted, a["+(i-1)+"] > a["+i+"]");
}
}
}
As Thomas comments, using the middle element as the pivot should work fine. It's a common choice, actually, because it works well with input arrays that happen to be already fully or partially sorted.
As for avoiding stack overflow, a common approach is to only recurse on the shorter part after a partitioning step - this ensures at least a halving of the array being processed at each level, so e.g. a 1,000,000 element array will have a maximum call depth of roughly 20 ( log2(1,000,000) ).
So, instead of
private void sort(int[] a, int lo, int hi) {
if (lo < hi) {
int p = partition(a, lo, hi);
sort(a, lo, p - 1);
sort(a, p + 1, hi);
}
}
You do
private void sort(int[] a, int lo, int hi) {
while (lo < hi) {
int p = partition(a, lo, hi);
// recurse on smaller part, loop on larger part
if (((p - 1) - lo) > (hi - (p + 1))) {
sort(a, p + 1, hi);
hi = p - 1;
}
else {
sort(a, lo, p - 1);
lo = p + 1;
}
}
}
Shuffle the array before sorting with the method below seems to have fixed the issues i had aswell
public void shuffle(int[] a) {
int N = a.length;
Random randomGenerator = new Random();
for (int i = 0; i < N; i++) {
int r = i + randomGenerator.nextInt(N-i); // between i and N-1
int t = a[i]; a[i] = a[r]; a[r] = t;
}
}

Time-complexity of mergesort with alternative merge-function

I currently have this code:
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File(args[0]));
int amount = sc.nextInt();
int[] array = new int[amount];
for (int i = 0; i < amount; i++) {
array[i] = sc.nextInt();
}
System.out.println("FINAL ANSWER " + find(array, 0, array.length - 1));
}
static int find(int arr[], int l, int r) {
int answer = 0;
if (l < r) {
int m = (l + r) / 2;
int a = find(arr, l, m);
int b = find(arr, m + 1, r);
answer = a + b + answer(arr, l, m, r);
}
return answer;
}
static int answer(int arr[], int l, int m, int r) {
int ans = 0;
for (int i = m; i < r; i++) {
for (int j = l; j < m + 1; j++) {
if (arr[i + 1] > arr[j]) {
ans++;
}
}
}
return ans;
}
I know mergesort has a time-complexity of O(nlog(n)), however I've replaced the merge-function by a function with two for-loops. So is it now O(n^2) since l, m and r depend on n?
The function answer has a complexity of O( (r-m) * (m+1-l) ). The outer for-loop goes from m to r, thus r-m iterations. The inner for-loop goes from l to m+1, so m+1-l iterations.
Given the worst case l=0,r=n and m=n/2, the function can be approximated to O(n^2).
However you need to add the runtime of the function find. So the overall time complexity will be:
O(n^2 * log(n)).

Trouble outputting names in a backpack problem

I was trying to solve a problem based on value and weight. In the task i had to pick out the elements by their value and weight, and find the highest efficiency solution. I receive an answer, however i am having trouble outputting the elements that were used in order to get an answer.
I've tried creating a string in which i place the values, however it gives out an outofbounds error.
public static void main(String[] args) {
String z[] = new String[]{"a","b","c","d","e","f","g","h","l","m"};
int w[] = new int[]{10,2,4,6,8,1,7,11,4,5};
int c[] = new int[]{20,3,5,7,4,1,8,15,8,6};
int maxW = 50;
int n = c.length;
System.out.println("");
int a = Find(w,c,maxW,n,z);
System.out.println("max value is " + a);
}
static int max(int a, int b)
{
if(a>b)
{
return a;
}
return b;
}
public static int Find(int w[],int c[], int maxW,int n, String[]z)
{
int K[][] = new int[n + 1][maxW + 1];
String s = "";
// Build table K[][] in bottom up manner
for (int i = 0; i<= n; i++)
{
for(int j = 0; j<= maxW; j++)
{
if (i == 0 || j == 0)
{
K[i][j] = 0;
}
else if (w[i - 1]<= j)
{
K[i][j] = max(c[i - 1] + K[i - 1][j - w[i - 1]], K[i - 1][j]);
}
else
{
K[i][j] = K[i - 1][j];
}
}
}
return K[n][maxW];
}
}
i want to output the same index element in string z, as the index element that is used to find the efficiancy.
The ideal result would be something like this in a string:
a a a b c d e m
(Just an example)
Thank you in advance.

Void merge sort method java

So, my computer science teacher has told me to make every method here void, except for copyPartArray. I have no idea how to do this, when I try, the sort simply fails.
public static ArrayList<String> mergeSortHelper(ArrayList<String> a) {
int mid = a.size() / 2 - 1;
if (a.size() <= 1)
return a;
return merge(mergeSortHelper(copyPartArray(a, 0, mid)),
mergeSortHelper(copyPartArray(a, mid + 1, a.size() - 1)));
}
public static void mergeSort(ArrayList<String> a) {
ArrayList<String> x = mergeSortHelper(a);
for (int i = 0; i < a.size(); i++) {
a.set(i, x.get(i));
}
}
public static ArrayList<String> merge(ArrayList<String> a,
ArrayList<String> b) {
ArrayList<String> x = new ArrayList<String>(a.size() + b.size());
int aCount = 0;
int bCount = 0;
for (int i = 0; i < a.size() + b.size(); i++) {
if (aCount > a.size() - 1) {
for (int j = bCount; j < b.size(); j++) {
x.add(b.get(j));
}
break;
}
if (bCount > b.size() - 1) {
for (int j = aCount; j < a.size(); j++) {
x.add(a.get(j));
}
break;
}
if ((a.get(aCount)).compareTo(b.get(bCount)) < 0) {
x.add(a.get(aCount));
aCount++;
} else {
x.add(b.get(bCount));
bCount++;
}
}
return x;
}
public static ArrayList<String> copyPartArray(ArrayList<String> a, int s,
int e) {
ArrayList<String> x = new ArrayList<String>();
for (int i = s; i <= e; i++) {
x.add(a.get(i));
}
return x;
I have tried to change my mergeSort to:
public static void mergeSort(ArrayList<String> a) {
int mid = a.size() / 2 - 1;
if (a.size() <= 1)
return;
mergeSort(copyPartArray(a, 0, mid));
mergeSort(copyPartArray(a, mid + 1, a.size() - 1));
merge(a, copyPartArray(a, 0, mid),
copyPartArray(a, mid + 1, a.size() - 1));
}
and get rid of the mergeSortHelper all together.
Now I have:
public static void mergeSort(ArrayList<String> a, int start, int end) {
int mid = (start + end) / 2;
if (a.size() <= 1)
return;
mergeSort(a, start, mid);
mergeSort(a, mid + 1, end);
how would I incorporate my merge method into this?
copyPartArray is going to make a copy of the array so that's no good, your lecturer wants you to pass the array by reference and then also pass in the start/end (or start/length) integers. Try doing something like this:
public static void mergeSort(ArrayList<String> a, int start, int length) {
// refer to 'the array' as a[start] to a[start + length]
}
a will be passed by reference which means you don't need a return value.
So I would change your methods to take a start and length and get rid of copyPartArray all together, you can do your merging in-place on the one array.
I use this method in my blog post on Quicksort.

Quicksort. Exception in thread "main" java.lang.StackOverflowError

Good day! I have here a Java program that does the quicksort. It reads a file then sorts the first 10,000 words in it. I followed the pseudocode of Thomas Cormen in his Introduction to Algorithms, Second Ed.
import java.io.*;
import java.util.*;
public class SortingAnalysis {
public static int partition(String[] A, int p, int r) {
String x = A[r];
int i = p-1;
for (int j=p; j < r-1; j++) {
int comparison = A[j].compareTo(x);
if (comparison<=0) {
i=i+1;
A[i] = A[j];
}
}
A[i+1] = A[r];
return i+1;
}
public static void quickSort(String[] a, int p, int r) {
if (p < r) {
int q = partition(a, p, r);
quickSort(a, p, q-1);
quickSort(a, q+1, r);
}
}
public static void main(String[] args) {
final int NO_OF_WORDS = 10000;
try {
Scanner file = new Scanner(new File(args[0]));
String[] words = new String[NO_OF_WORDS];
int i = 0;
while(file.hasNext() && i < NO_OF_WORDS) {
words[i] = file.next();
i++;
}
long start = System.currentTimeMillis();
quickSort(words, 0, words.length-1);
long end = System.currentTimeMillis();
System.out.println("Sorted Words: ");
for(int j = 0; j < words.length; j++) {
System.out.println(words[j]);
}
System.out.print("Running time: " + (end - start) + "ms");
}
catch(SecurityException securityException) {
System.err.println("Error");
System.exit(1);
}
catch(FileNotFoundException fileNotFoundException) {
System.err.println("Error");
System.exit(1);
}
}
}
However, when I run the code, the console says
Exception in thread "main" java.lang.StackOverflowError
at SortingAnalysis.partition and quickSort
I thought that the error was just because of the large size (ie, 10000) so I decreased it to 100 instead. However, it still doesn't sort the first 100 words from a file, rather, it displays the 100th word 100 times.
Please help me fix the code. I'm new in Java and I need help from you guys. Thank you very much!
EDIT: I now edited my code. It doesn't have an error now even when the NO_OF_WORDS reaches 10000. The problem is it halts the wrong sequence.
You have two problems:
the loop in partition() should run to j <= r - 1, you are jumping out early.
You are not swapping elements. Try the following code:
public static int partition(String[] A, int p, int r) {
String x = A[r];
int i = p - 1;
for (int j = p; j <= r - 1; j++) {
int comparison = A[j].compareTo(x);
if (comparison <= 0) {
i = i + 1;
swap(A, i, j);
}
}
swap(A, i + 1, r);
return i + 1;
}
public static void swap(String[] a, int i, int j) {
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
Looking at the Quicksort algo of wikipedia, the partition algo is the following :
// left is the index of the leftmost element of the array
// right is the index of the rightmost element of the array (inclusive)
// number of elements in subarray = right-left+1
function partition(array, 'left', 'right', 'pivotIndex')
'pivotValue' := array['pivotIndex']
swap array['pivotIndex'] and array['right'] // Move pivot to end
'storeIndex' := 'left'
for 'i' from 'left' to 'right' - 1 // left ≤ i < right
if array['i'] < 'pivotValue'
swap array['i'] and array['storeIndex']
'storeIndex' := 'storeIndex' + 1
swap array['storeIndex'] and array['right'] // Move pivot to its final place
return 'storeIndex'
In your method you don't use the pivotIndex value, you base your pivotValue on the right index. You need to add this parameter to your method.
Following the wiki algo it should be like this :
public static int partition(String[] A, int p, int r, int pivotIdx) {
String x = A[pivotIdx];
String tmp = A[pivotIdx];
A[pivotIdx] = A[r];
A[r]=tmp;
int i = p;
for (int j=p; j < r; j++) {
int comparison = A[j].compareTo(x);
if (comparison<=0) {
tmp=A[i];
A[i] = A[j];
A[j]=tmp;
i++;
}
}
tmp=A[i];
A[i] = A[r];
A[r]=tmp;
return i;
}

Categories