Question

**C++ only, use standard library, no vectors Write a program to generate a list of 5000...

**C++ only, use standard library, no vectors

Write a program to generate a list of 5000 random numbers between 1 and 10,000 stored in an array.


Sorts:

Print out the middle 50 numbers of the original list to show the results.

Now sort the original numbers using bubble sort. Print out the number of swaps made.

Now sort the original numbers using selection sort. Print out the number of swaps made.

Now sort the original numbers using insertion sort. Print out the number of swaps made.

Now sort the original numbers using quick sort. Print out the number of swaps made.

       Searches:

             Now you are to search for 2000 different numbers in the array of 5000. You will generate a random number between 1 and 5000 which will be the subscript value to use in the array of 5000.   Select the number at that position (the random number subscript), then call your search routine below and search for that value, counting the number of probes needed to find the value.

Now generate 2000 random number between 1 and 5000, to be search one at a time.

Look up each number using a linear search on the sorted list.

Now generate 2000 random number between 1 and 5000

Look up each number using a binary search on a sorted list.

Determine the average number of probes for each search above.

Do the values represent the theoretical values?
               Print your values and the theoretical values.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Random number 1 to 10000 size 5k in array

#include<studio.h>

#include<conio.h>

int main()

{

int arr[5000];
int i;
for (int i = 0; i < 5000; i++)
{
   arr[i]=rand()%10000;
}

}

2. Bubble Sort

// Optimized implementation of Bubble sort
#include <stdio.h>

void swap(int *xp, int *yp)
{
   int temp = *xp;
   *xp = *yp;
   *yp = temp;
}

// An optimized version of Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;

int count=0;
for (i = 0; i < n-1; i++)
{
   swapped = false;
   for (j = 0; j < n-i-1; j++)
   {
       if (arr[j] > arr[j+1])
       {
       swap(&arr[j], &arr[j+1]);
       swapped = true;

count=count+1;
       }
   }

   // IF no two elements were swapped by inner loop, then break
   if (swapped == false)
       break;
}
}


int main()
{
  
   int n = sizeof(arr)/sizeof(arr[0]);
   bubbleSort(arr, n);
   printf("%d ", count);
   return 0;
}

3.Selection Sort
#include <bits/stdc++.h>
using namespace std;

int count=0;

void swap(int *xp, int *yp)
{

count=count+1;
   int temp = *xp;
   *xp = *yp;
   *yp = temp;
}

void selectionSort(int arr[], int n)
{
   int i, j, min_idx;

   // One by one move boundary of unsorted subarray
   for (i = 0; i < n-1; i++)
   {
       // Find the minimum element in unsorted array
       min_idx = i;
       for (j = i+1; j < n; j++)
       if (arr[j] < arr[min_idx])
           min_idx = j;

       // Swap the found minimum element with the first element
       swap(&arr[min_idx], &arr[i]);
   }
}
int main()
{
   int n = sizeof(arr)/sizeof(arr[0]);
   selectionSort(arr, n);
   printf("%d ", count);
   return 0;
}

4.Insertion Sort

#include<iostream>
using namespace std;
int insertionsort(int arr[], int s)
{
   int current,i,j,count=0;
   for(i=1;i<s;i++)
   {
       current=arr[i];
       for(j=i-1;j>=0;j--)
       {
           if(current<arr[j])
           {
               arr[j+1]=arr[j];
               count++;
           }
           else
               break;
       }
       arr[j+1]=current;
   }
   return count;
}
int main()
{
   int t,n,i,res;
   int arr[100000];
   cin>>t;
   while(t--)
   {
       cin>>n;
       for(i=0;i<n;i++)
       {
           cin>>arr[i];
       }
       res=insertionsort(arr,n);
       cout<<res<<endl;
       printf("%d\n",count );
   }
   return 0;
}

5. Quick Sort

void quicksort(int arr[],int first,int last)
{
int pivot,j,temp,i;
int _SwapCount = 0;

if(first<last)
{
pivot=first;
i=first;
j=last;

while(i<j)
{
while(arr[i]<=arr[pivot]&&i<last)
i++;
while(arr[j]>arr[pivot])
j--;
if(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}

temp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=temp;
_SwapCount++;
quicksort(arr,first,j-1);
quicksort(arr,j+1,last);

}
}

Add a comment
Know the answer?
Add Answer to:
**C++ only, use standard library, no vectors Write a program to generate a list of 5000...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

    LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...

  • DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them...

    DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them in three arrays, then sorts the arrays with Insertion Sort, Merge Sort, and Quick Sort, respectively. Augment the three sorting algorithms with counters and report the number of characteristic operations each performs in sorting the (same) random values. INPUT The program reads from the terminal the number of random integers to generate, a seed value for the pseudo-random number generator, and a character that...

  • Write a C++ program that does the following : Accepts a positive integer ( n )...

    Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following 1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20 2. In the second function : Using recursion, Search for...

  • Problem: Write a C++ program that does the following : Accepts a positive integer ( n...

    Problem: Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following In the first function: display elements of the array. Display the first 20 elements If the size is > 20 In the second function : Using recursion, Search for a...

  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Please write a Java program that does the following: Create an array of 100 integers. Store...

    Please write a Java program that does the following: Create an array of 100 integers. Store 100 random integers (between 1 and 100) in the array. Print out the elements of the array. Sort the array in ascending order. Print out the sorted array. Prompt the user to enter a number between 1 and 100. Search the array for that number and then display "Found" or "Not Found" message. Display each number from 1 to 100 and the number of...

  • Description: Write a complete C++ program that will do the following: 1. Declare an array of...

    Description: Write a complete C++ program that will do the following: 1. Declare an array of 30 elements 2. Set the array to random numbers between 0 to 100 inclusive. Don't seed the random number generator. 3. Output the array. 4. Sort the array in ascending order using the selection sort. 5. Output the sorted array. Required IO: Array by F. Last Original: 1 5 ... 2 10 + Sorted: 1 2 ... 5 10 ( +

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT