Question

In: Computer Science

A template (generic) function for insertion sort is given to you. Part-1: design a function to...

A template (generic) function for insertion sort is given to you.

Part-1: design a function to compare 2 strings using case-insensitive

comparison,

a function to test if 2 records are not equal (using case-insensitive

string comparison).

Part-2: define your own compare function required by the template

insertionSort function.

#include <iostream>

#include <string>

#include <cstdlib>

#include <iomanip>

#include <ctime>

#define BASE 1000000000LL // long long int

using namespace std;

struct record

{

long long value;

string str;

};

// Part-1: case-insensitive string comparison, and operator!=()

bool operator!=(const record& r1, const record& r2)

{

// Test if r1 is NOT equal to r2.

// Strings are compared using case-insensitive comparison.

return true; // dummy return statement

}

// Part-2: compare function required by the template insertionSort and bubbleSort

functions

// Part-3: your template bubble sort function

// Part-4: compare function required by qsort

template<class Type>

void insertionSort(Type *x, int n, int (*compare)(const Type&, const Type&))

{

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

{

Type t = x[i];

int j;

for (j = i-1; j >= 0 && compare(x[j], t) > 0; j--)

x[j+1] = x[j];

x[j+1] = t;

}

}

void test_insertionSort(record *a, int n)

{

// Sort the list of record by value, and then by length of str,

// and then by str value using case-insensitive comparison.

clock_t begin, end;

double elapsedTime;

cout << "insertion sort: n = " << n << endl;

begin = clock();

// *** add your statement to sort the array a[] using insertionSort

end = clock();

elapsedTime = (double)(end - begin)/CLOCKS_PER_SEC;

cout << "Elapsed time = " << elapsedTime << " seconds" << endl << endl;

}

Solutions

Expert Solution

#include <iostream>

#include <string>

#include <cstdlib>

#include <iomanip>

#include <ctime>

#define BASE 1000000000LL // long long int

using namespace std;

struct record

{

long long value;

string str;

};

// Part-1: case-insensitive string comparison, and operator!=()

bool operator!=(const record& r1, const record& r2)

{
string str1 = r1.str;
string str2 = r2.str;
char ch1,ch2;

if(str1.length()!=str2.length())return false;
else {
   for(int i=0;i<str1.length();i++){
       ch1 = str1[i];
       ch2 = str2[i];
       if(ch1>='A'&&ch1<='Z')ch1=ch1-'A'+'a';
       if(ch2>='A'&&ch2<='Z')ch2=ch2-'A'+'a';
      
       if(ch1!=ch2)return false;
   }
}

return true; // dummy return statement

}

// Part-2: compare function required by the template insertionSort and bubbleSort
template<class Type>
int compare (const Type& r1, const Type& r2){
   if(r1.value > r2.value) return 1;
   return 0;
}

// Part-3: your template bubble sort function

template<class Type>

void bubbleSort(Type *x, int n, int (*compare)(const Type&, const Type&))

{

for(int i=n-1;i>=0;i--){
       for(int j=0;j<i;j++){
           if(compare(x[j], x[j+1]) > 0){
               Type temp=x[j];
               x[j]=x[j+1];
               x[j+1]=temp;
           }
       }
   }

}

// Part-4: compare function required by qsort

template<class Type>

void insertionSort(Type *x, int n, int (*compare)(const Type&, const Type&))

{

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

{

Type t = x[i];

int j;

for (j = i-1; j >= 0 && compare(x[j], t) > 0; j--)

x[j+1] = x[j];

x[j+1] = t;

}

}

void test_insertionSort(record *a, int n)

{

// Sort the list of record by value, and then by length of str,

// and then by str value using case-insensitive comparison.

clock_t begin, end;

double elapsedTime;

cout << "insertion sort: n = " << n << endl;

begin = clock();

// *** add your statement to sort the array a[] using insertionSort
insertionSort(a, n,&compare);

end = clock();

elapsedTime = (double)(end - begin)/CLOCKS_PER_SEC;

cout << "Elapsed time = " << elapsedTime << " seconds" << endl << endl;

}


Related Solutions

Function Template and Exception Handling In part one, you are going to implement the selection sort...
Function Template and Exception Handling In part one, you are going to implement the selection sort function that is capable of sorting vectors of int, double or string. In part two you will be writing a try catch block to catch the out-of-range exception. You are to write three functions and manipulate main() function for this lab all of which should be written in one main.cpp file: Part one: unsigned min_index(const vector<T> &vals, unsigned index): Passes in an index of...
Design two function templates as follows: - First function template will be used to sort arrays...
Design two function templates as follows: - First function template will be used to sort arrays of different data types in either descending or ascending order. This function template is required to have the following parameters: an array of a generic type, an integer representing the size of the array, and a character representing the order of sorting (i.e., ascending or descending). The last argument (i.e., the order of sorting) is required to be a default argument. The default order...
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Data Structure and Algorithm: 1. You are asked to use insertion sort to sort a singly...
Data Structure and Algorithm: 1. You are asked to use insertion sort to sort a singly linked list in ascending order. Why is this a bad idea? How does insertion sort’s runtime complexity change if you were to use it to sort a linked list? 2. Your classmate thinks that input arrays to the merge operation of merge sort don’t need to be in sorted order. Is your classmate right or wrong? Why?
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1...
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1 and 2, please: a. Let the program generate a random array. b. Output both the original random array and the sorted version of it
develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations,...
develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations, Insertion Sort – implement algorithms Permutations (Johnson Trotter): {1, 2, 3, 4, 5}; Insertion Sort: {45, 24, 16, 92, 71, 69, 28} develop count of # data “insertions” and develop # of key compares against the following: 16, 77, 24, 92, 44
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
1. Insertion sort for 12, 2, 3, 21, 11, 10,8 2. Bubble sort for 12, 2,...
1. Insertion sort for 12, 2, 3, 21, 11, 10,8 2. Bubble sort for 12, 2, 3, 21, 11, 10,8 3. selection sort for 12, 2, 3, 21, 11, 10,8 analysis of algorithm
Question: Consider the following variation of insertion sort: For 1 ≤ i < n, to insert...
Question: Consider the following variation of insertion sort: For 1 ≤ i < n, to insert the element A[i] among A[0] ≤ A[1] ≤ … ≤ A[i-1], do a binary search to find the correct position for A[i]. a. How many key comparisons would be done in the worst case? b. How many times are elements moved in the worst case? c. What is the asymptotic order of the worst case running time?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT