Question

C++ Programing Consider the following sorting techniques: • Bubble Sort • Insertion Sort • Selection Sort...

C++ Programing

Consider the following sorting techniques:

• Bubble Sort

• Insertion Sort

• Selection Sort

• Merge Sort

• Quick Sort

Select any two of the above sorting techniques:

1. Write an algorithm (pseudocode) for the two selected sorting techniques

2. Write two separate C++ programs using user defined functions for each of the selected sorting techniques.

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

Ques 1.

-------------------Quick Sort Algorithm -------------------

Inside the quickSort(arr, left, right) function:

arr is the array to be sorted.

left is the left bound of arr.

right is the right bound of arr.

  1. Check if left is less than right. If yes go to step 2, else exit the function.
  2. Create a partition using method partition().
  3. Call the quickSort() function recursively in the left subarray from index left to partition - 1.
  4. Call the quickSort() function recursively in the right subarray from index partition + 1 to right.

Inside the partition arr, left, right) function:

arr is the array to be sorted.

left is the left bound of arr.

right is the right bound of arr.

  1. Set pivot to the last element.
  2. Set i = left - 1.
  3. Loop from j <- left to right - 1
  • If arr[ j ] <= pivot, increment i and swap arr[ i ] and arr[ j ]
  1. Loop ends
  2. swap arr[ i + 1 ] and arr[ right ]
  3. return ( i + 1 )

-----------------------Insertion Sort Algorithm---------------------------

function insertionSort(arr , n)

BEGIN

    // if there is only 1 element in array

    if n <= 1

         return

    // recursively sort the first (n - 1) elements

    insertionSort(arr , n - 1);

    x = n - 1

    temp = arr[n]

    while x >= 0 and arr[x] > temp

    BEGIN

         arr[x + 1] = arr[x]

         j = j - 1

    END

    arr[j + 1] = temp

END

Ques 2.

-----------------Quick Sort----------------

#include<iostream>

#include<fstream>

#include<cstdlib>

using namespace std;

void quicksort(int *,int,int);

int partition(int *,int,int);

int main()

{

    int arr[] = { 8, 7, 6, 5, 4, 3, 2, 1 };

    int n = 8;

    int i;

       

    cout<<"Unsorted Array : ";

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

        cout<<arr[i]<<" ";

       

    quicksort(arr, 0, n - 1);

    cout<<"\nSorted Array : ";

   

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

        cout<<arr[i]<<" ";

       

    return 0;

}

// function to sort using quick sort

void quicksort(int *arr,int p,int r)

{

    int q;

   

    if(p<r)

    {

        // create a partition

        q = partition(arr,p,r);

       

        // sort the left subarray

        quicksort(arr,p,q-1);

        

        // sort the right subarray

        quicksort(arr,q+1,r);

    }

}

int partition(int *arr,int p,int r)

{  

    // set pivot

    int pivot = arr[r];

   

    // set the lindex of lower element

    int j, i = p - 1;

    for (j = p; j <= r - 1; j++ )

    {

        if (arr[j] <= pivot)

        {

            // increment smaller element index

            i++;

           

            // swap the contents of i and j position

            int temp=arr[i];

            arr[i]=arr[j];

            arr[j]=temp;

        }

    }

   

    int temp = arr[i + 1];

    arr[i+1] = arr[r];

    arr[r] = temp;

   

    return i + 1;

}


Sample Output

Unsorted Array87 6543 2 1 Sorted Array 1 2 3 4567 8

--------------------Insertion Sort---------------------

#include<iostream>

#include<fstream>

#include<cstdlib>

using namespace std;

void insertion_sort(int a[], int n);

int main()

{

    int arr[] = { 8, 7, 6, 5, 4, 3, 2, 1 };

    int n = 8;

    int i;

       

    cout<<"Unsorted Array : ";

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

        cout<<arr[i]<<" ";

       

    insertion_sort(arr, n);

    cout<<"\nSorted Array : ";

   

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

        cout<<arr[i]<<" ";

       

    return 0;

}

void insertion_sort(int a[], int n)

{

    int i, j, temp;

       

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

    {

        // store the current element in temp

        temp = a[i];

       

        // set j to i - 1

        j = i - 1;

        // all elements greater than temp are

        // shifted 1 position right

        while (j >= 0 && a[j] > temp)

        {

            // shifted 1 position right

            a[j+1] = a[j];

            j--;

        }

       

        a[j + 1] = temp;

    }

}


Sample Output

Unsorted Array87 6543 2 1 Sorted Array 1 2 3 4567 8​​​​​​​

Add a comment
Know the answer?
Add Answer to:
C++ Programing Consider the following sorting techniques: • Bubble Sort • Insertion Sort • Selection Sort...
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
  • Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort...

    Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort and Quick sort. For each sort you may store the numbers in an array or a linked list (this may be different for each sort). Write your program so that it accepts arguments from the command line using argc and argv in the main function call.

  • . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply...

    . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply that to the following array. Show your work in Detail. [15 points] 45 20 50 10 80 30 60 70 40 90 2. Is Shell sort a stable sorting algorithm? Answer this with an example. [10 points] 3. Apply Merge Sort to sort the following list. Show your work in Detail. [15 Points] 45 20 50 10 80 30 60 70 40 90 4....

  • c++ data structures please Pick any inefficient sorting algorithm you want, selection, bubble, insertion, etc., and...

    c++ data structures please Pick any inefficient sorting algorithm you want, selection, bubble, insertion, etc., and implement it as a function. Using the system clock as a timer, determine when the efficiency of the algorithm breaks down. For example, does it become slow after 1000 elements, 10000 elements, 100000 elements? Write some code to figure this out. Then use the sort() algorithm that is part of the STL <algorithm> library. How does this function compare to the function you implemented?...

  • Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection...

    Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection Sort with arrays of varying lengths. You will time each algorithm. The algorithms' time complexities should allow us to predict how these algorithms will perform. Program needs Four separate arrays, each with the same length and filled with the same sequence of randomly chosen numbers. Four separate functions, one for each of the four algorithms. All four functions will need to be timed. Be...

  • need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge...

    need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot - first index) algorithms. a) Compute the CPU processing time for all the algorithms for varying input sizes as follows: N-10, 103, 10, 10, and 106 b) Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 10, 1 -10, 1 - 10, 1 12 10 c) Write down your results...

  • 8 Sorting Algorithms: Bubble, selection, insertion, quick, merge, bucket, radix, counting. 1. A..Which of the above...

    8 Sorting Algorithms: Bubble, selection, insertion, quick, merge, bucket, radix, counting. 1. A..Which of the above sorting algorithms does TimSort use? 2. Which of the above algorithms sort a REVERSE ORDER list in O(n2 ) (worst case)? 3. Which of the above algorithms sort a REVERSE ORDER list in O(nlogn) (worst case)? 4. Which of the above algorithms sort an ordered list , a reverse ordered list, and a random list (all three) in 0(nlogn) (worst case)? 5. Which of...

  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • c++ Implement Radix Sort Most sorting algorithms, like bubble, insertion, selection and shell follow similar implementations....

    c++ Implement Radix Sort Most sorting algorithms, like bubble, insertion, selection and shell follow similar implementations. Radix sort is a unique sorting algorithm. In this assignment, implement the Radix Sort algorithm, as explained the text book in chapter 2. Use the Numbers.txt file in the DataFiles folder for the numbers to sort. Extra credit is available for processing alphabetic strings instead of just numbers. Specification: * Using your Doubly-Linked list to create an dynamic array of Doubly-Linked lists (like lab1)....

  • Write a program in Java that obtains the execution time of selection sort, insertion sort, bubble...

    Write a program in Java that obtains the execution time of selection sort, insertion sort, bubble sort, merge sort, quick sort, and radix sort. Your program should test all sort methods for input sizes of 10000, 20000, 30000, 40000, 50000, and 60000. The selection sort, bubble sort, and radix sort should also be tested for input sizes 100000 and 200000. Your program should create the data that is sorted from randomly generated integers and should output the results in a...

  • TO DO: IMPLEMENT SELECTION SORT, BUBBLE SORT, MERGE SORT INSTRUCTIONS: GENERATE AN ARRAY arr AND FILL...

    TO DO: IMPLEMENT SELECTION SORT, BUBBLE SORT, MERGE SORT INSTRUCTIONS: GENERATE AN ARRAY arr AND FILL IT WITH 100 RANDOM INTEGERS, HAVING VALUES 0-99. PRINT THE UNSORTED ARRAY IMPLEMENT EACH SORTING ALGORITHM (DO NOT USE THE BUILT-IN LIBRARIES) FOR EACH ALGORITHM, INCLUDE PSEUDOCODE WITH NUMBERED STEPS. IN YOUR CODE, CLEARLY COMMENT WHICH STEP IS BEING PERFORMED BY THE LINE OR BLOCK OF CODE. USE A TIMER TO CHECK HOW LONG EACH ALGORITHM TAKES TO SORT THE ARRAY. THIS SHOULD BE...

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