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...
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.
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...
C++ Given 2 int arrays that are related, sort them correspondingly. EXAMPLE: int arr1[] = {84,...
C++ Given 2 int arrays that are related, sort them correspondingly. EXAMPLE: int arr1[] = {84, 4, 30, 66, 15}; int arr2[] = {7, 5, 2, 9, 10}; SORTED ANSWER: 4 - 5 15 - 10 30 - 2 66 - 9 84 - 7 IN C++
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.
Using the buildHeap method, write a sorting function that can sort a list in O(nlogn) time....
Using the buildHeap method, write a sorting function that can sort a list in O(nlogn) time. def buildHeap(self,alist): i = len(alist) // 2 self.currentSize = len(alist) self.heapList = [0] + alist[:] while (i > 0): self.percDown(i) i = i - 1 Base on this code please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT