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...
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?
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?
In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection...
In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection sort and Bubble Sort. The objective of this project is to understand the relation between the theoretical asymptotic complexities (Big-O) computed in class and the actual running times for different kinds of input. The enclosed source file main.cpp contains a program that reads two inputs: a letter specifying which sorting algorithm to use (I for InsertionSort , S for SelectionSort and B for BubbleSort),...
1) Submit your completed dynamic array with template/generic data type (DArray.java) implementation. 2) Complete the template...
1) Submit your completed dynamic array with template/generic data type (DArray.java) implementation. 2) Complete the template DArray (if you have not done so) and this attached Iterator class. Make sure to document and test your code thoroughly. import java.util.Arrays; public class DArray { private final double GROW_FACTOR = 0.5;// array size growing rate //attributes private int size; private int buffer[]; //the actual array //constructors public DArray() { this.size=10; } public DArray(int size) throws Exception { if(size < 0) { throw...
1) Use insertion sort to sort: 25, 17, 31, 13, 2. Show your work step-by-step. 2)...
1) Use insertion sort to sort: 25, 17, 31, 13, 2. Show your work step-by-step. 2) Use Euclidean algorithm to find gcd(248, 198) 3) (ABCDEF)16 to binary, octal and decimal
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT