Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quicksort Mod(s) #12

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions CC_QuickSort/CC_QuickSort.pde
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand All @@ -84,7 +81,7 @@ void swap(float[] arr, int a, int b) {
float temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;


redraw();
}
83 changes: 83 additions & 0 deletions Merge Sort/MergeSort.pde
Original file line number Diff line number Diff line change
@@ -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++];
}
}