1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
int cntr;
int passed_miliseconds;
void merge(int numbers[], int temp[], int left, int mid, int right){
int i, left_end, num_elements, tmp_pos;
left_end = (mid - 1);
tmp_pos = left;
num_elements = (right - left + 1);
while ((left <= left_end) && (mid <= right))
{
if (numbers[left] <= numbers[mid])
{
temp[tmp_pos] = numbers[left];
tmp_pos += 1;
left += 1;
}
else
{
temp[tmp_pos] = numbers[mid];
tmp_pos += 1;
mid += 1;
}
}
while (left <= left_end)
{
temp[tmp_pos] = numbers[left];
left += 1;
tmp_pos += 1;
}
while (mid <= right)
{
temp[tmp_pos] = numbers[mid];
mid += 1;
tmp_pos += 1;
}
//modified
for (i=0; i < num_elements; i++)
{
numbers[right] = temp[right];
right -= 1;
}
}
void m_sort(int numbers[], int temp[], int left, int right){
int mid;
if (right > left)
{
mid = (right + left) / 2;
m_sort(numbers, temp, left, mid);
m_sort(numbers, temp, (mid+1), right);
for(int i=0;i<cntr-1;i++){
if(i==mid+1)
cout<<" I ";
cout<<numbers[i]<<" ";}cout<<endl;
merge(numbers, temp, left, (mid+1), right);
}
}
void mergeSort(int numbers[], int temp[], int array_size){
m_sort(numbers, temp, 0, array_size - 1);
}
/*int file_read(char a[], bool first)
{
string reader;
int i=0;
ifstream myfile("test2.txt");
if(!myfile)
{
cout<<"Error opening output file"<<endl;
system("pause");
}
else
{
if(first==true)
{
cntr=0;
while(!myfile.eof())
{
myfile>>reader;
cntr++;
}
return cntr;
}
else
{
while(!myfile.eof())
{
myfile>> a[i];
i++;
}
}
}
}*/
void swap(int a[],int b[],string c,string d,bool x,int s)//swaps string to ascii for sorting, then back
{
if(x){
for (int i = 0; i < s; i++)
{
a[i]=c[i];
b[i]=d[i];
}//end for
}
else{
for (int i = 0; i < s; i++)
{
c[i]=a[i];
d[i]=b[i];
}//end for
}
}
/*void sub_print(char a[])//print the middles. Do before 25th.
{
int temp;
}*/
int main()
{
int size=0;
int *translator1,*translator2;
string buffer, array1;
cout<<"Enter sumthin"<<endl;
cin>>array1;
cntr=array1.length();
translator1= new int[size];
translator2= new int[size];
swap(translator1,translator2,array1,buffer,true,size);
long int before = GetTickCount();
mergeSort(translator1, translator2, size);
long int after = GetTickCount();
swap(translator1,translator2,array1,buffer,false,size);
for (int i = 1; i < size; i++)
{
cout << array1[i] << " ";
}//end for
cout<<"\nExecution Time: "<<(after-before)<<" ms.\n";
return 0;
}//end main
|