Question

In: Computer Science

Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort,...

Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort, insertion sort, and merge sort,

It must show how long it took and how many movements occurred.

Please write codes in C++

Here's data set (should be stored in txt file)

7426
4524
4737
9436
3997
2757
6288
5414
9590
5968
6638
3199
9514
1541
9866
2144
6731
911
2171
6135
6437
912
9417
2662
6606
6349
707
2890
5386
9718
3492
5068
9674
8578
8323
7789
4748
7576
2664
6352
7967
8556
4740
5737
6764
368
1070
3700
1291
5279
9429
9507
2575
3099
2147
9660
2515
2976
4086
8305
6913
1308
7123
7678
8971
7507
139
51
5980
1100
3976
7289
9249
1662
8659
2758
3605
1079
7829
2298
3671
8901
1176
9089
3350
7500
6702
8903
5279

Solutions

Expert Solution

#include<iostream>
#include<fstream>
#include<list>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
#include<time.h>

using namespace std;
static int cm,cb,ci,cr;//variable for counting the number of moves
//display method
void display(int *array, int size)
{
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
//radix sort
void radixSort(int *arr, int n, int max)
{
int i, j, m, p = 1, index, temp, count = 0,s=0;
list<int> q[10]; //radix of decimal number is 10
for(i = 0; i< max; i++) {
m = pow(10, i+1);
p = pow(10, i);
for(j = 0; j<n; j++) {
temp = arr[j]%m;
index = temp/p; //find index for pocket array
q[index].push_back(arr[j]);
}
count = 0;
for(j = 0; j<10; j++) {
//delete from linked lists and store to array
while(!q[j].empty()) {
arr[count] = *(q[j].begin());
q[j].erase(q[j].begin());
count++;cr++;//count number of moves
}
}
}

}
//swapping method
void swapping(int &a, int &b) { //swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;
}
//merge() method
void merge(int *array, int l, int m, int r)
{
int i, j, k, nl, nr;
//size of left and right sub-arrays
nl = m-l+1; nr = r-m;
int larr[nl], rarr[nr];
//fill left and right sub-arrays
for(i = 0; i<nl; i++)
larr[i] = array[l+i];
for(j = 0; j<nr; j++)
rarr[j] = array[m+1+j];
i = 0; j = 0; k = l;
//marge temp arrays to real array
while(i < nl && j<nr) {
if(larr[i] <= rarr[j]) {
array[k] = larr[i]; cm++; //count the number of moves
i++;
}else{
array[k] = rarr[j]; cm++;//count the number of moves
j++;
}
k++;
}
while(i<nl) { //extra element in left array
array[k] = larr[i]; cm++;//count the number of moves
i++; k++;
}
while(j<nr) { //extra element in right array
array[k] = rarr[j]; cm++;//count the number of moves
j++; k++;
}
}
//recursive method to mergesort
void mergeSort(int *array, int l, int r) {
int m;
if(l < r) {
int m = l+(r-l)/2;
// Sort first and second arrays
mergeSort(array, l, m);
mergeSort(array, m+1, r);
merge(array, l, m, r);
}

}
//insertion sort
void insertionSort(int *array, int size) {
int key, j;
for(int i = 1; i<size; i++) {
key = array[i];//take value
j = i;
while(j > 0 && array[j-1]>key) {
array[j] = array[j-1]; ci++; //count the number of moves
j--;
}
array[j] = key; //insert in right place
}

}

void bubbleSort(int *array, int size) {
     
for(int i = 0; i<size; i++) {
int swaps = 0; //flag to detect any swap is there or not
for(int j = 0; j<size-i-1; j++) {
if(array[j] > array[j+1]) { //when the current item is bigger than next
swapping(array[j], array[j+1]); cb++;
swaps = 1; //set swap flag
}
}
if(!swaps)
break; // No swap in this pass, so array is sorted
}

}
//driver program
int main()
{
    int a[200],b[200],c[200],d[200],x,i=0,n;
    ifstream in;
    clock_t begin, end;
double tr,tm,ti,tb;
    //open the files in read mode
   in.open("D://data.txt");
   //if not open
   if(!in)
   {
      cout<<endl<<"Unable to open the source file";
          exit(0);
   }
     
   //read the strings from file
   while(! in.eof())
        {
        in>>x;
        a[i]=x; //store the number in four arrays
        b[i]=x;
        c[i]=x;
        d[i]=x;
           i++;  
       }
       n=i;
   cout<<endl<<"\t\tRADIX SORT\n";
cout <<endl << "Data before Sorting: ";
display(a, n);
//clock begins
    begin = clock();
radixSort(a, n, 4);
end = clock(); //clock ends
//total time taken
tr = (double)(end - begin)/CLOCKS_PER_SEC;

cout <<endl<<"Data after Sorting: ";
display(a, n);


cout<<endl<<"\t\tMERGE SORT\n";

cout <<endl<< "Data before Sorting: ";
display(b, n);
//clock begins
begin = clock();
mergeSort(b, 0, n-1);
end = clock();//clock ends
//total time taken
tm = (double)(end - begin)/CLOCKS_PER_SEC;
cout <<endl<< "Data after Sorting: ";
display(b, n);

cout<<endl<<"\t\tINSERTION SORT\n";
cout <<endl<< "Data before Sorting: ";
display(c, n);
//clock begins
begin = clock();
insertionSort(c, n);   
end = clock();//clock ends
//total time taken
ti = (double)(end - begin)/CLOCKS_PER_SEC;
cout <<endl<< "Data after Sorting: ";
display(c, n);
cout<<endl<<"\t\tBUBBLE SORT\n";
cout <<endl<< "Data before Sorting: ";
display(d, n);
//clock begins
begin = clock();
bubbleSort(d, n);   
end = clock();//clock ends
//total time taken
tb = (double)(end - begin)/CLOCKS_PER_SEC;
cout <<endl<< "Data after Sorting: ";
display(d, n);
//display the times taken by sorting methods
    cout<<endl<<"Total time taken by Radix Sort is : "<<tr;
    cout<<endl<<"Total time taken by merge Sort is : "<<tm;
    cout<<endl<<"Total time taken by insertion Sort is : "<<ti;
    cout<<endl<<"Total time taken by bubble Sort is : "<<tb;
    //display the moves during the sorting methods          
cout<<endl<<cr<<" Number of moves used during radix sort.";
cout<<endl<<cm<<" Numbers of moves used during merge sort.";
cout<<endl<<cb<<" Numbers of moves used during bubble sort.";
cout<<endl<<ci<<" Numbers of moves used during insertion sort.";
  
}

OUTPUT


Related Solutions

Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort,...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort, and selection sort. It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674 8578 8323...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a data set (txt file) then do the sorting algorithm to measure how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from the current directory a list of intergers (10 numbers to be exact), and the sort them, and print them to the screen. You can use redirection to read data from a given file through standard input, as opposed to reading the data from the file with the read API (similar to Lab #1). You can assume the input data will only have 10 numbers...
Develop an algorithm and implement Multilevel Queuing scheduling using C++.and using bubble sorting. Consider a set...
Develop an algorithm and implement Multilevel Queuing scheduling using C++.and using bubble sorting. Consider a set of user and system processes and determine (i) turnaround time (ii) waiting time of each process (iii) Average Waiting Time (AWT) and (iv) Average Turnaround Time (ATAT) of all processes. please write comment
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much...
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much like the previous lab, you will be tasked with prompting the user for a list of values until the user enters 0 (you may use the same initializeVector that you wrote in the last lab). You will then write bubblesort which sorts the vector from smallest to largest. You will then call a function displayVector which displays the vector to the screen. Keep everything...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into ascending order. Use the Bubble Sort algorithm from the lecture. You can use either Base Addressing or Indexed Addressing for the arrays. For this assignment, make sure you prompt the user for the numbers. Do not hard-code them in the data section. NOTE: Declare the array last in the Data section.
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting .Arrival time, burst time and priority values.The code should also display: (i) Gantt chart and determine the following: (ii) Determine the Turnaround time(TAT), waiting time(WT) of each process (iii) Determine the Average Waiting Time (AWT) and Average Turnaround Time (ATAT) of all processes. please write the comments
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
In Java: Write two advanced sorting methods of your choice: (Shell Sort OR Radix Sort) AND...
In Java: Write two advanced sorting methods of your choice: (Shell Sort OR Radix Sort) AND (Merge Sort OR Quick Sort). If you choose Shell Sort, experiment with different incremental sequences to see how they affect the algorithm's run time efficiency (count the number of comparisons and exchanges). If you choose to implement Radix Sort, answer the following question as well: Can you write a version of Radix Sort for String objects? If yes, explain how. If not, explain why....
What is the number of comparisons in the bubble sort algorithm, if it is used to...
What is the number of comparisons in the bubble sort algorithm, if it is used to sort a list of n-entries? Justify your formula.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT