Question

In: Computer Science

Write a very general sort method that can sort any type of data arrays/lists. For example,...

Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language doesn’t support these features, implement a revised version (clearly state the revision you made to the problem with a brief discussion of the language’s weakness.) Then test the following cases:

(a) 4, 3, 7, 2, 1, 9 in ascending order

(b) 4.5, 2.0, 9.0, 8.4, 7.2, 6.1, 20.5, 2.1 in descending order

(c) John 40, Casey 45, Ben 47, Zadi 41, Kay 39, Tay 43 in ascending order of names

(d) John 40, Casey 45, Ben 47, Zadi 41, Kay 39, Tay 43 in descending order of scores

Include your program source code and test cases (may copy paste output or show screenshots) as answer to any coding problem.

Solutions

Expert Solution

// CPP program to sort array of any data types.
#include <bits/stdc++.h>

using namespace std;

  
// Template formed so that sorting of any  
// type variable is possible

template <class T>

void sortArray(T a[], int n)
{   

// boolean variable to check that  

// whether it is sorted or not

bool b = true;

while (b) {

b = false;

for (size_t i=0; i<n-1; i++) {

  

// swapping the variable

// for sorting order

if (a[i] > a[i + 1]) {

T temp = a[i];

a[i] = a[i + 1];

a[i + 1] = temp;

b = true;

}

}

}
}

  
// Template formed so that sorting of any  
// type variable is possible

template <class T>

void printArray(T a[], int n)
{

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

cout << a[i] << " ";

cout << endl;
}

  
// Driver code

int main()
{

int n = 4;

int intArr[n] = { 2000, 456, -10, 0 };

sortArray(intArr, n);

printArray(intArr, n);

  

string strArr[n] = { "We do nothing",

"Hi I have something",

"Hello Join something!",

"(Why to do work)" };

sortArray(strArr, n);

printArray(strArr, n);

  

float floatArr[n] = { 23.4, 11.4, -9.7, 11.17 };

sortArray(floatArr, n);

printArray(floatArr, n);

  

return 0;


Related Solutions

USE JAVA. Write a very general sort method that can sort any type of data arrays/lists....
USE JAVA. Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language...
Code: C++ Write a very general sort method that can sort any type of data arrays/lists....
Code: C++ Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language...
A very common application of arrays is computing statistics on lists of numbers. Below you will...
A very common application of arrays is computing statistics on lists of numbers. Below you will find a template of a program that uses four user-defined methods: input an array of numbers, compute the average the array, find the largest number in the array, and find the smallest number in the array. Enter the following program into C# (ignore the line numbers) and replace the lines marked with // *** with the appropriate C# code (may require more than one...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these 2 Arrays are equal or not, and also if the elements are all even numbers. Describe under what conditions these 2 Arrays would be considered equal.
Sets can be though of as lists that don't contain any repeated elements. For example, [a,4,6]...
Sets can be though of as lists that don't contain any repeated elements. For example, [a,4,6] is a set, but [a,4,6,a] is not (as it contains 2 occurrences of a). Write a Prolog program, powerset/2 that generates the set of all subsets of a list in sorted order. (Note the input list may not, itself, be a set.) Example1: powerset([a,b],X). should yield: X = [[],[a],[a,b],[b],[b,a]]. Example2: powerset([a,b,a],X). should also yield (note input list is not actually a set): X =...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
Write a function in any functional programming language that will reverse a general list. For example,...
Write a function in any functional programming language that will reverse a general list. For example, if an input is (A (B C (D E)) F), output is (F ((E D) C B) A).  Please note that any built-in/pre-defined function, e.g., reverse, cannot be used in your answer. Please DO NOT hard-code any input values, output values in your code. Please submit a screenshot of where your code got compiled, executed, showing the execution result
Write a program that reads and parses the CSV file, and can sort and filter data...
Write a program that reads and parses the CSV file, and can sort and filter data according to the user input and print the results. Your program should be able to do the following according to the user input: 1. Show results from a specific country 2. Sort the data according to any column 3. Filter the results (for example, >10000 cases) 4. Print the top or bottom n results You get bonus points if your program • Can do...
Of Temperature class Arrays are a very powerful data structure with which you must become very...
Of Temperature class Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The an object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many...
A 2-3-4 tree can be used as a sorting machine. Write a sort() method that has...
A 2-3-4 tree can be used as a sorting machine. Write a sort() method that has passed an array of key values from main() and writes them back to the array in sorted order.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT