Question

Write a version of the selection sort algorithm that can be used to sort a list...

Write a version of the selection sort algorithm that can be used to sort a list of strings alphabetically. (Selection sort for int lists is discussed in Chapter 8.) Write a program to test the function and prompt the user to enter 10 strings. Output the sorted list to the console. *Need answer in C++*

Homework Answers

Answer #1

//Write a program to implement selection sort for strings.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#define MAX_LENGTH 80

//length of all string should be smaller than MAX_LENGTH
void selection_Sort(char array[][MAX_LENGTH], int num) //function definition
{
int i, j, min_idx;
char minString[MAX_LENGTH];
for (i = 0; i < num-1; i++)
{
   // Find the smallest element in unsorted array
   int min_idx = i;
   strcpy(minString, array[i]); //copy the contents of array[i] to minString
   for (j = i + 1; j < num; j++)
   {
   // If minimum is greater than array[j]
   if (strcmp(minString, array[j]) > 0) //compare both the strings character by character
   {
       // Make array[j] as minString and update the value of min_idx
       strcpy(minString, array[j]);
       min_idx = j;
   }
   }
   // Interchange the found minimum element with the 1st element
   if (min_idx != i)
   {
   char temp[MAX_LENGTH];
   strcpy(temp, array[i]); //Interchange item[position] and item[i]
   strcpy(array[i], array[min_idx]);
   strcpy(array[min_idx], temp);
   }
}
}

void main()
{
char array[10][MAX_LENGTH];
int i,num =10;
clrscr();
for (i = 0; i < num; i++)
{
   cout<<"Enter the string:"<<i+1<<"=";
   cin>>array[i]; //accept 10 strings from user
}
selection_Sort(array, num); //calling selection_Sort function

cout << "\nSorted array is\n";
for (i = 0; i < num; i++)
   cout << i+1 << ": " << array[i] << endl; //display sorted array
getch();
}

Output:

Screenshots of the program:

Please give positive rating.

Know the answer?
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
Similar Questions
Write a version of the bubble sort algorithm in a function called bubbleSort that can be...
Write a version of the bubble sort algorithm in a function called bubbleSort that can be used to sort a string vector object. Also, write a program to test your algorithm. The program should prompt the user for a series of names. The string zzz should end the input stream. Output the sorted list to the console. *need answer in C++*
Write an application that prompt user to choose Sorting Algorithm (Insertion, Merge, or Heap) in GUI...
Write an application that prompt user to choose Sorting Algorithm (Insertion, Merge, or Heap) in GUI input message, then prompt user to enter numbers in GUI input message also, sort these numbers in descending order using Selected sorting algorithm, and show the result in message dialog as shown in the following figure. The program contains two classes: 1- “Sort” Class which contains 3 methods named as follow: a. InsertionSort(int[] numbers), returns sorted numbers from the passed array using Insertion sort...
Write an insertion sort algorithm in C language by performing following steps .make sure code is...
Write an insertion sort algorithm in C language by performing following steps .make sure code is work. thanks Prompt the user to enter the number of array elements (say, N). Read the number of elements (N). Implement the insertion sort algorithm (note that insertion sort is an in-place sort that modifies the original array). Print the sorted array.
The binary search algorithm given in this chapter is nonrecursive. Write and implement a recursive version...
The binary search algorithm given in this chapter is nonrecursive. Write and implement a recursive version of the binary search algorithm. The program should prompt Y / y to continue searching and N / n to discontinue. If an item is found in the list display the following message: x found at position y else: x is not in the list Use the following list of integers when testing your program: 2, 6, 8, 13, 25, 33, 39, 42, 53,...
The bubble sort algorithm discussed in class is used to sort the following sequence of integers:...
The bubble sort algorithm discussed in class is used to sort the following sequence of integers: 47 29 1 9 5 23 • How many passes must the algorithm perform to guarantee the entire sequence is sorted? • What is the list obtained after the first pass? • What is the list obtained after the third pass? • What is the list obtained after the final pass?
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...
In this programming exercise you will create an algorithm for solving the following version of the...
In this programming exercise you will create an algorithm for solving the following version of the m Smallest Numbers problem.   Instead of just returning the m smallest values as in homework 1, you want to return a list of the positions where the m smallest values are located without changing the original array. Your algorithm should meet the following specifications: mSmallest( L[1..n], m ) Pre: L is a list of distinct integer values. n is the number of elements in...
Write a function to accept a header of a sorted linked and a name which can...
Write a function to accept a header of a sorted linked and a name which can be used as a key. If the key is found then add the record to the linked list using pointer(s). Do not write complete program. Write just one function. I just need a function that can demonstrate the prompt above.