Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include using namespace std; void selectionSort(unsigned int arr[], int size){ int i, j, temp; for(i=0; i

#include

#include

#include

using namespace std;

void selectionSort(unsigned int arr[], int size){

int i, j, temp;

for(i=0; i

{

for(j=i+1; j

{

if(arr[i]>arr[j])

{

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

}

}

}

}

void Merge(unsigned int *a, int low, int high, int mid)

{

int i, j, k;

unsigned int temp[high-low+1];

i = low;

k = 0;

j = mid + 1;

while (i <= mid && j <= high){

if (a[i] < a[j]){

temp[k] = a[i];

k++;

i++;

}

else{

temp[k] = a[j];

k++;

j++;

}

}

// Insert all the remaining values from i to mid unsigned into temp[].

while (i <= mid){

temp[k] = a[i];

k++;

i++;

}

// Insert all the remaining values from j to high unsigned into temp[].

while (j <= high){

temp[k] = a[j];

k++;

j++;

}

// Assign sorted data stored in temp[] to a[].

for (i = low; i <= high; i++){

a[i] = temp[i-low];

}

}

// A function to split array unsigned into two parts.

void MergeSort(unsigned int *a, int low, int high)

{

int mid;

if (low < high)

{

mid=(low+high)/2;

// Split the data unsigned into two half.

MergeSort(a, low, mid);

MergeSort(a, mid+1, high);

// Merge them to get sorted output.

Merge(a, low, high, mid);

}

}

void MaxHeapify(unsigned int a[], int i, int n)

{

int j;

unsigned int temp;

temp = a[i];

j = 2*i;

while (j <= n)

{

if (j < n && a[j+1] > a[j])

j = j+1;

// Break if parent value is already greater than child value.

if (temp > a[j])

break;

// Switching value with the parent node if temp < a[j].

else if (temp <= a[j])

{

a[j/2] = a[j];

j = 2*j;

}

}

a[j/2] = temp;

return;

}

void HeapSort(unsigned int a[], int n)

{

int i;

unsigned int temp;

for (i = n; i >= 2; i--)

{

// Storing maximum value at the end.

temp = a[i];

a[i] = a[1];

a[1] = temp;

// Building max heap of remaining element.

MaxHeapify(a, 1, i - 1);

}

}

void Build_MaxHeap(unsigned int a[], int n)

{

int i;

for(i = n/2; i >= 1; i--)

MaxHeapify(a, i, n);

}

unsigned int partitionQ(unsigned int a[],unsigned int l,unsigned int u)

{

int i,j;

unsigned int v, temp;

v=a[l];

i=l;

j=u+1;

do

{

do

i++;

while(a[i]

do

j--;

while(v

if(i

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

}while(i

a[l]=a[j];

a[j]=v;

return(j);

}

void quick_sort(unsigned int a[], int l, int u)

{

int j;

if(l

{

j=partitionQ(a,l,u);

quick_sort(a,l,j-1);

quick_sort(a,j+1,u);

}

}

// Get maximum value from array.

unsigned int getMax(unsigned int arr[], int n)

{

unsigned int max = arr[0];

for ( int i = 1; i < n; i++)

if (arr[i] > max)

max = arr[i];

return max;

}

// Count sort of arr[].

void countSort(unsigned int arr[], unsigned int n, unsigned int exp)

{

// Count[i] array will be counting the number of array values having that 'i' digit at their (exp)th place.

unsigned int output[n];

int i, count[10] = {0};

// Count the number of times each digit occurred at (exp)th place in every input.

for (i = 0; i < n; i++)

count[(arr[i] / exp) % 10]++;

// Calculating their cumulative count.

for (i = 1; i < 10; i++)

count[i] += count[i-1];

// Inserting values according to the digit '(arr[i] / exp) % 10' fetched unsigned into count[(arr[i] / exp) % 10].

for (i = n - 1; i >= 0; i--)

{

output[count[(arr[i] / exp) % 10] - 1] = arr[i];

count[(arr[i] / exp) % 10]--;

}

// Assigning the result to the arr pounsigned inter of main().

for (i = 0; i < n; i++)

arr[i] = output[i];

}

// Sort arr[] of size n using Radix Sort.

void radixsort(unsigned int arr[], unsigned int n)

{

unsigned int exp, m;

m = getMax(arr, n);

// Calling countSort() for digit at (exp)th place in every input.

for (exp = 1; m/exp > 0; exp *= 10)

countSort(arr, n, exp);

}

int main()

{

unsigned int a[100000],b[100000];

int i;

for ( i = 0; i < 100000; ++i){

a[i] = rand();

b[i] = a[i];

}

clock_t tStart = clock();

selectionSort(a,100000);

cout << "Selection Sort Time Taken: " << (double)(clock()-tStart)/CLOCKS_PER_SEC*1000 << " ms." << endl;

for ( i = 0; i < 100000; ++i){

a[i] = b[i];

}

tStart = clock();

MergeSort(a,0,100000);

cout << "Merge Sort Time Taken: " << (double)(clock()-tStart)/CLOCKS_PER_SEC*1000 << " ms." << endl;

for ( i = 0; i < 100000; ++i){

a[i] = b[i];

}

tStart = clock();

HeapSort(a,100000);

cout << "Heap Sort Time Taken: " << (double)(clock()-tStart)/CLOCKS_PER_SEC*1000 << " ms." << endl;

for ( i = 0; i < 100000; ++i){

a[i] = b[i];

}

tStart = clock();

quick_sort(a,0,100000);

cout << "Quick Sort Time Taken: " << (double)(clock()-tStart)/CLOCKS_PER_SEC*1000 << " ms." << endl;

for ( i = 0; i < 100000; ++i){

a[i] = b[i];

}

tStart = clock();

radixsort(a,100000);

cout << "Radix Sort Time Taken: " << (double)(clock()-tStart)/CLOCKS_PER_SEC*1000 << " ms." << endl;

return 0;

}

In the main, a random function to generate data is used. How can i implement a text file called "numbers.txt" that will run each routine containing 10 million 32-bit integers? Followed by writing the sorted result to an output file call sortedNumbers.txt Thank you, will rate. Example of the numbers.txt file ---------------------------------------------------------

36788515 2608640614 2886320747 4036781246 3321890980 2847340631 86726398 1310966122 3081072471 585227411 3180692180 384584385 3703421471 368817869 533907302 1480973500 2632244756 1251635025 2227717148 2052748823 1611506273 2912989838 2085192398 405858412 1733380901 2456955357 2351611177 4162635137 944362868 2159438134 501484330 482326880 1928442953 3330359748 2997021312 1718845843 1777139306 2266376630 4125265671 3176182088 1433730396 2115987675 3330567214 1000305309 2023492960 794737233 3463014262 1355838638 288800337 3136295750 3230213075 1425225429 1780884887 234372760 3582580258 2226118883 3896004895 2816605579 2664829599 3720468538 1769786176 2886950255 3359179584 3555279375 3474989002 2015883882 3757340495 1449697941 163467714 1990567687 3176610718 503105833 2151756410 1714999549 2648124464 3142596996 691046773 3446487775 2427022985 1576419827 781385045 3537818288 1222133032 3420778105 1950199246 838315460 3990809588 2170947846 3635768982 2216825155 45936042 1905940138 2289800016 2525900770 4220609102 1600602564 1005665418 72895665 2578417321 1190273325 3081180567 3444503204 2788891809 3483061348 1731752844 555610461 591267005 1047407835 1789379897 387305169 2336456836 3777526770 3995419655 2320653809 929598980 1772215903 612619760 3532139123 2214317092 835062842 3153399926 2435279205 3415234605 3759846970 628378752 1260134130 3499627568 1688950755 2257828709 118106371 2910766713 3330025142 2916576451 2818891512 3812262390 3113635431 3784258931 1019016799 2657666996 1970055669 3777980302 4215561356 3404124490 3285789188 4045491012 557757802 233206850 3231061990 1227311327 1889545985 1049366160 1094542810 3173757851 2162121301 1962863629 1571896385 4170720691 2628499703 1208128713 287437766 607411519 1680715701 1360426139 1803201685 1316784672 3954937439 2399685493 1264890498 1852964111 2399689819 3204896502 414768707 1492658002 820779636 1145250995 2004034448 2159467500 529672560 3053977027 1798798450 3629891952 1429177811 2890128162 3812385099 2944787680 1295616116 3803763496 651165220 3793182150 640343124 1249832953 440161490 839512819 1202673964 573071084 3562765349 600354975 1163555632 2891311364 2087857182 890199864 904851381 2630427745 3905013525 3325545318 922848998 3557478048 4117368823 2383662823 1430084209 2171912289 200392033 4031515279 2434877767 1137830313 3543407080 238305582 942139424 1617807196 2145444162 3927917461 1250604911 2831008156 3177876069 1966955874 629140942 643949399 2810851019 3756864275 702173141 634215045 3425779355 77201630 2539637966 3076403669 2918724054 1159146154 1416269884 1055568628 3207358097 2993163528 3660527819 702800390 1295408144 3610115859 4251444151 1675854358 1419016986 4234769757 3415653822 2714702473 3042548476 1088566788 884852899 459945370 3223286755 1526828396 3112994289 3413110700 2474520035 1561621598 3592894125 3458543571 1108294996 1413650128 1632395264 4134932788 953072611 432548866 2552251616 831800719 3727519425 786927413 240583888 3506313354 3782519274 4214000094 989007753 1395293240 852246584 1162886741 3648691618 3263925910 464105649 1049732087 3618758220 1127876556 1875296416 4169499641 130714104 3875996119 1319416524 2641880930 1328450069 1611172047 1836862386 1127736828 2309121456 2848089184 1639174811 2961163846 3641516844 1085770322 3822665707 3971898960 2577813234 8235472 2199898177 4068058578 2998261539 1079459843 3243046055 1545431278 2076339253 1180018011 255621058 1666845769 3998488275 2833220630 2714824715 184177117 1546236490 3067422645 2540488431 3409710844 1642396787 2116752724 1709923592 2561196860 1314384451 2297584948 152417119 2640707673 3055874434 3909424411 3919626600 2099155770 1812920200 2852678584 4104169597 2068209100 2509935831 1109050692 3682923463 1543492107 341663193 416895156 4102624655 1466760664 4221231246 . . . . . . 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions