Question

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.

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

Hi Friend, Implementing all sorting algorithm in one post is too big.

I have implemented Selction Sort.

Please repost for other in separate post.


#include <iostream>
using namespace std;


void swap(int &xp, int &yp)
{
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]);
}
}

/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
cout<<arr[i]<<" ";
cout<<endl;
}

// Driver program to test above functions
int main()
{
int arr[] = {56, 12, 34, 64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
cout<<"Sorted array: \n";
printArray(arr, n);
return 0;
}

fourthweek fourthweek fourthweek fourthweek g++ selectionSort.cpp fourthweek ./a.out Sorted array: 11 12 12 22 25 34 56 64 fo

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

  • C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick...

    C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick Sort. Program must: Read an array size from the user, dynamically an array of that size, and fill the array with random numbers Sort the array with the Insertion Sort, MergeSort and QuickSort algorithms studied in class, doing a time-stamp on each sort. Use your program to measure and record the time needed to sort random arrays of size 5000, 50000, and 500000. For...

  • 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...

  • . 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....

  • USING C++ write a program that creates a modified version of Merge sort in which insertion...

    USING C++ write a program that creates a modified version of Merge sort in which insertion sort will be used for merging array elements.

  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

    Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...

  • Program this in C There is a lot of sorting algorithms. So far, we have covered...

    Program this in C There is a lot of sorting algorithms. So far, we have covered selection and insertion sort. However, we only covered how selection sort is implemented using an array. Every sorting algorithm implemented using an array can also be implemented using a linked list. In this assignment, you will implement the selection sort algorithm using a linked list instead of an array. You will first create an interface for handling string items. Basically, you will need to...

  • struct student { char name[10]; int rank; }; Using the student structure given above, create an...

    struct student { char name[10]; int rank; }; Using the student structure given above, create an array of size 5 students.. Then write a complete C program to sort the students array based on the students rank. Use the following sorting techniques in your code. Don’t forget to print initial array and final (sorted) array. a. Selection sort b. Insertion sort c. Merge sort / Quick sort.

  • 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...

  • 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...

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