From 7362d5f32fe7089e7eeb7b5cb09582e5ab24b38c Mon Sep 17 00:00:00 2001 From: Tushar Sharma Date: Mon, 14 Jan 2019 19:17:43 +0530 Subject: [PATCH 1/2] MergeSort added Help! Suggestion required for animation. --- Merge Sort/MergeSort.pde | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Merge Sort/MergeSort.pde diff --git a/Merge Sort/MergeSort.pde b/Merge Sort/MergeSort.pde new file mode 100644 index 0000000..895016f --- /dev/null +++ b/Merge Sort/MergeSort.pde @@ -0,0 +1,83 @@ +float[] values; + +int count = 0; + +void setup() { + size(600, 400); +// fullScreen(); + values = new float[width]; + for (int i = 0; i < values.length; i++) { + values[i] = random(height); + } +} + +void draw() { + background(0); + + if(count < width){ + mergeSort(values, 0, values.length-1); + } + + for (int i = 0; i < values.length; i++) { + stroke(255); + line(i, height, i, height - values[i]); + } + + count++; +} + +void mergeSort(float[] arr, int p, int r) +{ + if(p < r) { + int q = (p + r)/2; + mergeSort(arr, p, q); + mergeSort(arr, q+1, r); + merge(arr, p, q, r); + } + else{ + return; + } +} + +void merge(float[] arr, int p, int q, int r) +{ + int n1 = q - p + 1; + int n2 = r - q; + + float[] leftArr = new float[n1]; + float[] rightArr = new float[n2]; + + int i, j, k; + + for( i = 0; i < n1; i++ ) + leftArr[i] = arr[p + i]; + + for( j = 0; j < n2; j++ ) + rightArr[j] = arr[q + j + 1]; + + i = j = 0; + k = p; + + while (i < n1 && j < n2) + { + if (leftArr[i] <= rightArr[j]) + { + arr[k] = leftArr[i++]; + } + else + { + arr[k] = rightArr[j++]; + } + k++; +} + + while (i < n1) + { + arr[k++] = leftArr[i++]; + } + + while (j < n2) + { + arr[k++] = rightArr[j++]; + } +} From df9323ba05169306adbeb86d68da25b3b071d04f Mon Sep 17 00:00:00 2001 From: Tushar Sharma Date: Sun, 27 Jan 2019 16:26:37 +0530 Subject: [PATCH 2/2] QuickSort Bug updated Mod(s) - partition code updated - the second while removed with swap condition - last swap condition modified - return statement changed --- CC_QuickSort/CC_QuickSort.pde | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/CC_QuickSort/CC_QuickSort.pde b/CC_QuickSort/CC_QuickSort.pde index b6ca0a1..8cc66cd 100644 --- a/CC_QuickSort/CC_QuickSort.pde +++ b/CC_QuickSort/CC_QuickSort.pde @@ -48,27 +48,24 @@ int partition(float[] arr, int lo, int hi) { //println("Partition " + lo + " to " + hi); float pivot = arr[hi]; int left = lo-1; - int right = hi-1; + int right = lo; - while (left <= right) { - left++; + while (right <= hi-1) { println(left, right); - if (arr[left] >= pivot) { - while (right > left) { - if (arr[right] < pivot) { - swap(arr, left, right); - break; - } - right--; + if (arr[right] <= pivot) { + left++; + if(arr[left] != arr[right]){ + swap(arr, left, right); + break; } } + right++; } - if (left < hi-1) { - swap(arr, left, hi); - } + swap(arr, left+1, right); + println("Mid: "+ left); - return left; + return left+1; } @@ -84,7 +81,7 @@ void swap(float[] arr, int a, int b) { float temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; - - + + redraw(); }