the output of this program in c #include #include #include #include #include
Posted: Sun Jul 10, 2022 11:27 am
the output of this program in c
#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <string.h>#include <time.h>
void getRandomArray(int arr[], int n, int max); //initiate an arraywith random numbersvoid cloneArray(int arrB[], int arrA[], int size); //copy an arrayto anothervoid printArray(int arr[], int size); //print all elements of anarrayvoid swap(int *xp, int *yp);void bubbleSort(int arr[], int n); //bubble sortvoid selectionSort(int arr[], int n); //selection sortvoid insertionSort(int list[], int n); //insertion sort
int main(int argc, char *argv[]) { int SIZE, MAX; printf("Enter SIZE and MAX: "); scanf("%d%d", &SIZE, &MAX);
//int arrA[SIZE], arrB[SIZE]; int* arrA = (int*)malloc(SIZE *sizeof(int)); int* arrB = (int*)malloc(SIZE *sizeof(int)); puts("\nGet an array: "); getRandomArray(arrA, SIZE, MAX); cloneArray(arrB, arrA, SIZE); printArray(arrA, SIZE);
clock_t start = clock(); bubbleSort(arrB, SIZE); printf("Time = %f ms\n",1000.0*(clock()-start)/CLOCKS_PER_SEC); printArray(arrB, SIZE);
cloneArray(arrB, arrA, SIZE); start = clock(); selectionSort(arrB, SIZE); printf("Time = %f ms\n",1000.0*(clock()-start)/CLOCKS_PER_SEC); printArray(arrB, SIZE);
cloneArray(arrB, arrA, SIZE); start = clock(); insertionSort(arrB, SIZE); printf("Time = %f ms\n",1000.0*(clock()-start)/CLOCKS_PER_SEC); printArray(arrB, SIZE); free(arrA); free(arrB);}
//=======================================================
void getRandomArray(int arr[], int n, int max) { srand((int)clock()); for (int i = 0; i < n; i++) { arr = rand() % max; }}
void cloneArray(int arrB[], int arrA[], int size) { for (int i = 0; i < size; i++) arrB = arrA;}
void printArray(int arr[], int size) { for (int i=0; i < size; i++) printf("%d ", arr); printf("\n");}
void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp;}
//=======================================================
void bubbleSort(int arr[], int n) { puts("\n==== Bubble Sort "); int comp = 0, swp = 0; bool swapped; for (int i = 0; i < n-1; i++) { swapped = false; for (int j = 0; j < n-i-1;j++) { if (arr[j]> arr[j+1]) { swap(&arr[j], &arr[j+1]); swapped = true; swp++; } comp++; } if (swapped == false)break; } printf("No. comparisons = %d; No. swaps = %d\n",comp, swp);}
void selectionSort(int arr[], int n) { puts("\n==== Selection Sort "); int comp = 0, swp = 0; int min_idx; for (int i = 0; i < n-1; i++) { min_idx = i; for (int j = i+1; j < n;j++) if (arr[j]< arr[min_idx]) { min_idx = j; comp++; } swap(&arr[min_idx],&arr); swp++; } printf("No. comparisons = %d; No. swaps = %d\n",comp, swp);}
void insertionSort(int list[], int n) { puts("\n==== Insertion Sort "); int comp = 0, assg = 0; for (int h = 1; h < n; h++) { int key = list[h]; int k = h - 1; //startcomparing with previous item while (k >= 0 &&key < list[k]) { list[k +1] = list[k]; comp++; assg++; --k; } list[k + 1] = key; } printf("No. comparisons = %d; No. assignments =%d\n", comp, assg);} //end insertionSort
Write a program in C, which compares the performance of 4sorting algorithms. Each algorithm sorts n numbers from an inputfile containing a list of numbers. Each program then sorts nnumbers and prints the numbers to a file. You Should use theimplementations of the sorting algorithms from the classnotes.1. Insertion sort algorithm(G1).2. Selection sort algorithm(G2).3. Quick sort algorithm(G3).4. Heap-sort algorithm (G4).b. Dataset 1: Run your program on values from 1 to n where n=10,000 (i.e. input numbers are sorted and there is no need to readfrom an input file). Print the execution time on the screen withwell-explained messages for each algorithm.c. Dataset 2: Read the first 10,000 entries only found in“test_dat.txt” and Run your program. Print the sorted input andexecution time to 4 output files called (G1.txtx, G2.txt, G3.txtx,and G4.txt).
dataset 2 read from the file and writes the output to the fileas mentioned.
the data stored in test_dat.txt like below, these five numbersneed to be sorted.
26838157582011327515
please don't copy the previous answer it is wrong.410511562733010174192621214086
Enter SIZE and MAX: 100 10 Get an array: 39 5 8 6 6 5 0 8 1 4 6 7 0 6 4 8 8 0 2 3 5 7 29 0 3 3 7 7 6 1 7 2 9 3 3 3 0 7 4 6 1 8 3 3 4 5 6 1 5 1 8 5 1 6 7 1 26 2 0 3 4 6 9 1 3 3 8 1 8 4 1 4 9 9 3 4 3 3 1 6 2 5 5 39 59 59 7 2 5 6 0 4 6 7 ==== Bubble Sort No. comparisons = 4914; No. swaps = 2198 Time=0.000000 ms 0 0 0 0 0 0 0 1 1 1 1 1 1 1 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 ====Selection Sort No. comparisons = 191; No. swaps = 99 Time=0.000000 ms |||| 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 999999999 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 ==== Insertion Sort No. comparisons = 2198; No. assignments = 2198 Time=0.000000 ms 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8999999999
#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <string.h>#include <time.h>
void getRandomArray(int arr[], int n, int max); //initiate an arraywith random numbersvoid cloneArray(int arrB[], int arrA[], int size); //copy an arrayto anothervoid printArray(int arr[], int size); //print all elements of anarrayvoid swap(int *xp, int *yp);void bubbleSort(int arr[], int n); //bubble sortvoid selectionSort(int arr[], int n); //selection sortvoid insertionSort(int list[], int n); //insertion sort
int main(int argc, char *argv[]) { int SIZE, MAX; printf("Enter SIZE and MAX: "); scanf("%d%d", &SIZE, &MAX);
//int arrA[SIZE], arrB[SIZE]; int* arrA = (int*)malloc(SIZE *sizeof(int)); int* arrB = (int*)malloc(SIZE *sizeof(int)); puts("\nGet an array: "); getRandomArray(arrA, SIZE, MAX); cloneArray(arrB, arrA, SIZE); printArray(arrA, SIZE);
clock_t start = clock(); bubbleSort(arrB, SIZE); printf("Time = %f ms\n",1000.0*(clock()-start)/CLOCKS_PER_SEC); printArray(arrB, SIZE);
cloneArray(arrB, arrA, SIZE); start = clock(); selectionSort(arrB, SIZE); printf("Time = %f ms\n",1000.0*(clock()-start)/CLOCKS_PER_SEC); printArray(arrB, SIZE);
cloneArray(arrB, arrA, SIZE); start = clock(); insertionSort(arrB, SIZE); printf("Time = %f ms\n",1000.0*(clock()-start)/CLOCKS_PER_SEC); printArray(arrB, SIZE); free(arrA); free(arrB);}
//=======================================================
void getRandomArray(int arr[], int n, int max) { srand((int)clock()); for (int i = 0; i < n; i++) { arr = rand() % max; }}
void cloneArray(int arrB[], int arrA[], int size) { for (int i = 0; i < size; i++) arrB = arrA;}
void printArray(int arr[], int size) { for (int i=0; i < size; i++) printf("%d ", arr); printf("\n");}
void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp;}
//=======================================================
void bubbleSort(int arr[], int n) { puts("\n==== Bubble Sort "); int comp = 0, swp = 0; bool swapped; for (int i = 0; i < n-1; i++) { swapped = false; for (int j = 0; j < n-i-1;j++) { if (arr[j]> arr[j+1]) { swap(&arr[j], &arr[j+1]); swapped = true; swp++; } comp++; } if (swapped == false)break; } printf("No. comparisons = %d; No. swaps = %d\n",comp, swp);}
void selectionSort(int arr[], int n) { puts("\n==== Selection Sort "); int comp = 0, swp = 0; int min_idx; for (int i = 0; i < n-1; i++) { min_idx = i; for (int j = i+1; j < n;j++) if (arr[j]< arr[min_idx]) { min_idx = j; comp++; } swap(&arr[min_idx],&arr); swp++; } printf("No. comparisons = %d; No. swaps = %d\n",comp, swp);}
void insertionSort(int list[], int n) { puts("\n==== Insertion Sort "); int comp = 0, assg = 0; for (int h = 1; h < n; h++) { int key = list[h]; int k = h - 1; //startcomparing with previous item while (k >= 0 &&key < list[k]) { list[k +1] = list[k]; comp++; assg++; --k; } list[k + 1] = key; } printf("No. comparisons = %d; No. assignments =%d\n", comp, assg);} //end insertionSort
Write a program in C, which compares the performance of 4sorting algorithms. Each algorithm sorts n numbers from an inputfile containing a list of numbers. Each program then sorts nnumbers and prints the numbers to a file. You Should use theimplementations of the sorting algorithms from the classnotes.1. Insertion sort algorithm(G1).2. Selection sort algorithm(G2).3. Quick sort algorithm(G3).4. Heap-sort algorithm (G4).b. Dataset 1: Run your program on values from 1 to n where n=10,000 (i.e. input numbers are sorted and there is no need to readfrom an input file). Print the execution time on the screen withwell-explained messages for each algorithm.c. Dataset 2: Read the first 10,000 entries only found in“test_dat.txt” and Run your program. Print the sorted input andexecution time to 4 output files called (G1.txtx, G2.txt, G3.txtx,and G4.txt).
dataset 2 read from the file and writes the output to the fileas mentioned.
the data stored in test_dat.txt like below, these five numbersneed to be sorted.
26838157582011327515
please don't copy the previous answer it is wrong.410511562733010174192621214086
Enter SIZE and MAX: 100 10 Get an array: 39 5 8 6 6 5 0 8 1 4 6 7 0 6 4 8 8 0 2 3 5 7 29 0 3 3 7 7 6 1 7 2 9 3 3 3 0 7 4 6 1 8 3 3 4 5 6 1 5 1 8 5 1 6 7 1 26 2 0 3 4 6 9 1 3 3 8 1 8 4 1 4 9 9 3 4 3 3 1 6 2 5 5 39 59 59 7 2 5 6 0 4 6 7 ==== Bubble Sort No. comparisons = 4914; No. swaps = 2198 Time=0.000000 ms 0 0 0 0 0 0 0 1 1 1 1 1 1 1 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 ====Selection Sort No. comparisons = 191; No. swaps = 99 Time=0.000000 ms |||| 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 999999999 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 ==== Insertion Sort No. comparisons = 2198; No. assignments = 2198 Time=0.000000 ms 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8999999999