Last active
March 1, 2016 20:35
-
-
Save tterrag1098/46b672970dc1c1257a8b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
gspicer-davis1@linux-jbi4:~/dev/COSC320/Lab4> cat makefile | |
OBJS = main.o | |
CC = g++ | |
DEBUG = -g | |
CFLAGS = -c -std=c++11 -Wall $(DEBUG) | |
LFLAGS = -Wall $(DEBUG) | |
EXE = Lab4 | |
$(EXE): $(OBJS) | |
$(CC) $(LFLAGS) $(OBJS) -o $@ | |
main.o: | |
$(CC) $(CFLAGS) main.cpp QuickSorter.cpp QuickSorter.h Sorter.h | |
clean: | |
rm *.o *.gch *~ $(EXE) |
This file contains hidden or 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
#include "QuickSorter.h" | |
QuickSorter::QuickSorter(Partition p) : type(p), Sorter<int>("Quicksort"){} | |
QuickSorter::~QuickSorter() | |
{ | |
} | |
void QuickSorter::sort(int *arr, int size) | |
{ | |
sort(arr, 0, size); | |
} | |
void QuickSorter::sort(int *arr, int p, int r) | |
{ | |
if (p < r) | |
{ | |
int q = partition(arr, p, r); | |
sort(arr, p, q); | |
sort(arr, q + 1, r); | |
} | |
} | |
int QuickSorter::partition(int *arr, int p, int r) | |
{ | |
int pivot = arr[r]; | |
int i = p - 1; | |
for (int j = i; op(), j < r; j++) | |
{ | |
op(); | |
if (arr[i] <= pivot) | |
{ | |
i++; | |
swap(arr[i + 1], arr[r]); | |
} | |
} | |
swap(arr[i + 1], arr[r]); | |
return i + 1; | |
} |
This file contains hidden or 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
#ifndef QUICK_SORTER_H | |
#define QUICK_SORTER_H | |
#include "Sorter.h" | |
#include <stddef.h> | |
enum Partition { recursive, iterative }; | |
class QuickSorter : public Sorter<int> | |
{ | |
public: | |
QuickSorter (Partition p); | |
~QuickSorter (); | |
void sort(int *arr, int size) override; | |
private: | |
void sort(int *arr, int p, int r); | |
int partition(int *arr, int p, int r); | |
Partition type; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment