Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Keith Perkins
- //CS 2420
- //Program 6
- //Sorting
- #include <string>
- #include <vector>
- #include <iostream>
- #include <fstream>
- #include <time.h>
- #include <stdio.h>
- #include <dos.h>
- #include "stdafx.h"
- using namespace std;
- int main()
- {
- string fileName1 = "file1.txt";
- string fileName2 = "file2.txt";
- string fileName3 = "file3.txt";
- vector<int> file1vec;
- vector<int> file2vec;
- vector<int> file3vec;
- int count1 = 0;
- int count2 = 0;
- int count3 = 0;
- //1. Read in file1 from the download area containing integers in text format
- ifstream iDataFile;
- iDataFile.open(fileName1);
- if (iDataFile.fail())
- {
- cout << "Invalid file 1." << endl;
- system("PAUSE");
- return 1;
- }
- //2. Count the number of elements in the file
- while (!(iDataFile.eof()))
- {
- int data;
- iDataFile >> data;
- file1vec.push_back(data);
- count1++;
- }
- iDataFile.close();
- iDataFile.open(fileName2);
- if (iDataFile.fail())
- {
- cout << "Invalid file 2." << endl;
- system("PAUSE");
- return 1;
- }
- while (!(iDataFile.eof()))
- {
- int data;
- iDataFile >> data;
- file2vec.push_back(data);
- count2++;
- }
- iDataFile.close();
- iDataFile.open(fileName3);
- if (iDataFile.fail())
- {
- cout << "Invalid file 3." << endl;
- system("PAUSE");
- return 1;
- }
- while (!(iDataFile.eof()))
- {
- int data;
- iDataFile >> data;
- file3vec.push_back(data);
- count3++;
- }
- //3. Obtain the starting clock tick value for the Insertion Sort
- //4. Sort the file with the Insertion Sort
- //5. Obtain the ending clock tick value for the Insertion Sort
- //6. Obtain the starting clock tick value for the Shellsort
- //7. Sort the file with the Shellsort
- //8. Obtain the ending clock tick value for the Shellsort
- //9. Obtain the starting clock tick value for the Quicksort
- //10. Sort the file with the Quicksort
- //11. Obtain the ending clock tick value for the Quicksort
- //12. Display the statistical information for the file for each sort(see example below).
- //13. Write the sorted file out to the disk with a new name with the first letter of the sort(i.e.I for the Insertion sort, S for the Shellsort, or Q for the Quicksort) followed by the number of the file(i.e.I1, I2, I3 for the Insertion Sorted files, S1, S2, S3 for the Shellsort, and Q1, Q2, Q3 for the Quicksort).Each number should be written as text with each number on a separate line in the file.
- //14. Repeat these steps for file2 and file3
- system("PAUSE");
- return 0;
- }
- void insertionSort(int a[], int l)
- {
- int j = 0;
- int temp;
- for (int i = i; i < l; i++)
- {
- j = i;
- while (j > 0 && a[j] < a[j - 1])
- {
- temp = a[j];
- a[j] = a[j - 1];
- a[j - 1] = temp;
- j--;
- }
- }
- }
- void shellsort(int a[], int l)
- {
- int gap, i, j, temp;
- for (gap = l / 2; gap > 0; gap /= 2)
- {
- for (i = gap; i < l; i++)
- {
- for (j = i - gap; j >= 0 && a[j]>a[j + gap]; j -= gap)
- {
- temp = a[j];
- a[j] = a[j + gap];
- a[j + gap] = temp;
- }
- }
- }
- }
- void quickSort(int a[], int start, int end)
- {
- int i = start, j = end;
- int temp;
- int pivot = a[(start + end) / 2];
- while (i <= j)
- {
- while (a[i] < pivot)
- i++;
- while (a[j] > pivot)
- j--;
- if (i <= j)
- {
- temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- i++;
- j--;
- }
- }
- if (start < j)
- quickSort(a, start, j);
- if (i < end)
- quickSort(a, i, end);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement