Created
May 4, 2016 19:17
-
-
Save AtharvaVaidya/170ecd7e2238f3e76cf6b46e6e43effa 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
// | |
// lab5.cpp | |
// CS171 | |
// | |
// Created by Atharva Vaidya on 2016-04-27. | |
// Copyright © 2016 Atharva Vaidya. All rights reserved. | |
// | |
//Initial runtime: 1e-06 | |
#include <iostream> | |
#include <ctime> | |
#include <vector> | |
#include <fstream> | |
using namespace std; | |
void swap(std::vector<string> & data, int i, int j) | |
{ | |
string tmp = data[i]; | |
data[i] = data[j]; | |
data[j] = tmp; | |
} | |
int Partition(std::vector<string> & words, int low, int high); | |
void QuickSort(std::vector<string> & words, int low, int high) | |
{ | |
if (low >= high) return; | |
int p = Partition(words, low, high); | |
QuickSort(words, low, p-1); | |
QuickSort(words, p+1, high); | |
} | |
int Partition(std::vector<string> & words, int low, int high) | |
{ | |
int p = low; | |
for (int i = p+1; i <= high; ++i) | |
{ | |
if (words[i] < words[p]) | |
{ | |
swap(words, i, p); | |
if (i != p+1) | |
{ | |
swap(words, i, p+1); | |
} | |
p = p + 1; | |
} | |
} | |
return p; | |
} | |
int main() | |
{ | |
clock_t start_time = clock(); | |
cout << "Enter a filename: "; | |
string filename; | |
cin >> filename; | |
ifstream in; | |
ofstream out("/Users/atharvavaidya/Developer/CS171/sorted_words.txt"); | |
in.open(filename.c_str()); | |
string current_word; | |
vector<string> words; | |
while (in >> current_word) | |
{ | |
words.push_back(current_word); | |
} | |
//insertion sort | |
for (int i = 0; i < words.size(); i++) | |
{ | |
string x = words[i]; | |
int j = i -1; | |
while (j >= 0 && words[j] > x) | |
{ | |
words[j+1] = words[j]; | |
j = j -1; | |
} | |
words[j + 1] = x; | |
} | |
//quicksort | |
//QuickSort(words, 0, words.size() - 1); | |
for (int i = 0; i < words.size(); i++) | |
{ | |
out << words[i] << endl; | |
} | |
clock_t stop_time = clock(); | |
double time_diff = double(stop_time-start_time)/CLOCKS_PER_SEC; | |
cout << time_diff << " seconds" << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment