Skip to content

Commit

Permalink
Added C implementation of QuickSort
Browse files Browse the repository at this point in the history
  • Loading branch information
ArunHarish committed Jun 30, 2018
1 parent 41c693d commit 869e82c
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 3 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified BubbleSort/BubbleSort.js
100644 → 100755
Empty file.
Empty file modified HeapSort/HeapSort.js
100644 → 100755
Empty file.
Empty file modified MergeSort/MergeSort.js
100644 → 100755
Empty file.
Empty file modified MergeSort/QueueList.js
100644 → 100755
Empty file.
Empty file modified Misc/LinkedNodes.js
100644 → 100755
Empty file.
Empty file modified QuickSort/QuickSort.js
100644 → 100755
Empty file.
Binary file added QuickSort/QuickSort/QuickSort
Binary file not shown.
115 changes: 115 additions & 0 deletions QuickSort/QuickSort/Quicksort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include "StackList.h"
#define MAX 100

// swap
void swap(int a, int b, int input[]) {
if(a != b) {
int temp = input[a];
input[a] = input[b];
input[b] = temp;
}
}

// comparator
int comparator(int rightValue, int pivotValue, int sortType) {
if(sortType == -1) {
return pivotValue < rightValue;
}
return rightValue < pivotValue;
}

// partition
int partition(int right, int pivot, int arrays[], int sortType) {
int left = right;
int pivotValue = arrays[pivot];
while(right < pivot) {
int rightValue = arrays[right];
if(comparator(rightValue, pivotValue, sortType)) {
swap(left, right, arrays);
left++;
}
right++;
}
swap(left, pivot, arrays);
return left;

}
// generatePosition
positionNode* generatePosition(int start, int end, int arrays[], int sortType) {
positionNode* ret = allocPosition();
ret->start = start;
ret->end = end;
ret->partition = partition(start, end, arrays, sortType);
return ret;
}

void QuickSort(int inputArray[], int comparator) {
// first partition
int size = MAX;
Stack* pIndice = allocStack();
append(pIndice, generatePosition(0, size - 1, inputArray, comparator));

int currentPivot;
positionNode *currentPos = allocPosition();
positionNode *firstPos = pIndice->currentNode->nodeValue;
// Copying first element
currentPos->start = firstPos->start;
currentPos->partition = firstPos->partition;
currentPos->end = firstPos->end;
firstPos = NULL;

while((currentPos)) {
int partition = currentPos->partition;
int leftStart = currentPos->start;
int leftEnd = partition - 1;
int rightStart = partition + 1;
int rightEnd = currentPos->end;
// Left Pivot
if(leftStart < leftEnd) {
append(pIndice, generatePosition(leftStart, leftEnd, inputArray, comparator));
}
// Right Pivot
if(rightStart < rightEnd) {
append(pIndice, generatePosition(rightStart, rightEnd, inputArray, comparator));
}

// This could be dangerous when memory is freed. Potential problem raising dangling pointers
// currentNode = pIndice->currentNode;
// Solution copy

if(pIndice->currentNode) {
positionNode *lastPos = pIndice->currentNode->nodeValue;
currentPos->start = lastPos->start;
currentPos->partition = lastPos->partition;
currentPos->end = lastPos->end;
lastPos = NULL;
}
else // explicitly they are assigning NULL
currentPos = NULL;

pop(pIndice);

}
// Free currentPos
freePosition(currentPos);
// Free the stack
freeStack(pIndice);
}

int main() {
// Creating a random number sequence
int random[MAX];

srand(time(NULL));
for(int x = 0; x < MAX; x++) {
random[x] = rand() % 10000;
}
QuickSort(random, 1);
for(int x = 0; x < MAX; x++) {
printf("%d\n", random[x]);
}
return 0;
}
Binary file added QuickSort/QuickSort/StackList
Binary file not shown.
69 changes: 69 additions & 0 deletions QuickSort/QuickSort/StackList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "StackList.h"
#include <stdio.h>
#include <stdlib.h>


void append(Stack* stackList, positionNode* appendValue) {
// Check whether stackList is empty
if(!(stackList->currentNode)) {
// Create a new node
Node *newNode = malloc(sizeof(Node));
newNode->nextNode = NULL;
newNode->prevNode = NULL;
newNode->nodeValue = appendValue;
stackList->currentNode = newNode;
return ;
}

Node * newNode = malloc(sizeof(Node));
newNode->nextNode = NULL;
newNode->prevNode = stackList->currentNode;
newNode->nodeValue = appendValue;
stackList->currentNode = newNode;
}

void pop(Stack* stackList) {
Node *currentNode = stackList->currentNode;
// If there is no element present then ignore
if(!(currentNode))
return ;
// Get the current node value
positionNode *nodeValue = currentNode->nodeValue;
// Set the stackList currentNode to be the prev node
Node *prevNode = currentNode->prevNode;
currentNode->prevNode = NULL;
currentNode->nextNode = NULL;
// If previous node is not NULL
if(prevNode)
prevNode->nextNode = NULL;
// Free up the space used by current node
free(currentNode);
// Free up the space used by node value
freePosition(nodeValue);
stackList->currentNode = prevNode;
}

Stack* allocStack() {
Stack *stackList = malloc(sizeof(Stack));
return stackList;
}

void freeStack(Stack* stackList) {
if(stackList) {
// Freeing stackList alone does not deallocates the node elements
// Hence pop all nodes
while(stackList->currentNode)
pop(stackList);
free(stackList);
}
}

positionNode* allocPosition() {
positionNode *node = malloc(sizeof(positionNode));
return node;
}

void freePosition(positionNode* freeingNode) {
if(freeingNode)
free(freeingNode);
}
36 changes: 36 additions & 0 deletions QuickSort/QuickSort/StackList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef STACK_H
#define STACK_H

/*
* @author Arun Harish Balasubramonian
*
*/
typedef struct Node Node;
typedef struct Stack Stack;
typedef struct positionNode positionNode;

typedef struct Node{
Node *nextNode;
Node *prevNode;
positionNode *nodeValue;
} Node;

typedef struct Stack {
Node *currentNode;
} Stack;

typedef struct positionNode {
int partition ;
int start ;
int end ;
} positionNode;

void append(Stack*, positionNode*);
void pop(Stack*);
Stack* allocStack(void);
void freeStack(Stack*);

positionNode* allocPosition();
void freePosition(positionNode*);

#endif
Empty file modified QuickSort/StackList.js
100644 → 100755
Empty file.
6 changes: 3 additions & 3 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var HeapSort = require("./HeapSort/HeapSort.js");
var BubbleSort = require("./BubbleSort/BubbleSort.js");

function generateRandom() {
var random = new Array(100000);
var random = new Array(1000000);
for(var x = 0; x < random.length; x++) {
random[x] = Math.floor(1 + Math.random() * 10000);
random[x] = Math.floor(1 + Math.random() * 99);
}
return random;
}
Expand Down Expand Up @@ -37,4 +37,4 @@ function test() {
console.log(check(quickSample, 1))
}

test();
test();

0 comments on commit 869e82c

Please sign in to comment.