forked from ArunHarish/Sorting-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41c693d
commit 869e82c
Showing
14 changed files
with
223 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters