Define a problem utilizing an efficient sorting algorithm please write code in c++ oop thnx?
One of the most primitive application of sorting is to apply a binary search on an array.
Below is the code in C++.
CODE
#include <cstdlib>
#include <iostream>
using namespace std;
// Implementation of efficient Quick sort
int partition(int arr[], int low, int high)
{
int pivot = arr[low];
int i = low - 1, j = high + 1;
while (true) {
do {
i++;
} while (arr[i] < pivot);
do {
j--;
} while (arr[j] > pivot);
if (i >= j)
return j;
swap(arr[i], arr[j]);
}
}
int partition_r(int arr[], int low, int high)
{
srand(time(NULL));
int random = low + rand() % (high - low);
swap(arr[random], arr[low]);
return partition(arr, low, high);
}
void quickSort(int arr[], int low, int high)
{
if (low < high) {
int pi = partition_r(arr, low, high);
quickSort(arr, low, pi);
quickSort(arr, pi + 1, high);
}
}
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int main()
{
int n = 10;
int arr[n];
srand(time(NULL));
for (int i=0; i<n; i++) {
arr[i] = rand() % 10;
}
cout << "The array is: " << endl;
for(int i=0; i<n; i++) {
cout << arr[i] << " ";
}
cout << endl;
quickSort(arr, 0, n - 1);
int index = binarySearch(arr, 0, n-1, 4);
cout << "4 found at location " << index;
return 0;
}
Define a problem utilizing an efficient sorting algorithm please write code in c++ oop thnx?
write code to demonstrate the features of oop use C# programming.
You are working as the software developer and need to identify the best sorting algorithm. Please refer to the following URL: https://www.geeksforgeeks.org/sorting-algorithms/ You can pick any two-sorting algorithm. You need to determine which sorting algorithm is the best to sort the array of 10k elements. Use the following steps to help with your solution: Create C++ functions for any two-sorting algorithm. Write down the random array generation function to generate at least 10k elements. Pass the same array into both...
Exercise 1: Sorting void mysort(vector & v); Write a function called mysort that rearranges elements of a vector v so that they form an increasing sequence of values. Allow for different vector positions to contain the same value. Develop your own sorting algorithm; do not use a predefined sort routine from the standard library. Implement a simple sorting algorithm rather than an efficient algorithm. Write code that thoroughly tests your function. Express your tests using assertions. Exercise 2: Comparison For...
c++ data structures please Pick any inefficient sorting algorithm you want, selection, bubble, insertion, etc., and implement it as a function. Using the system clock as a timer, determine when the efficiency of the algorithm breaks down. For example, does it become slow after 1000 elements, 10000 elements, 100000 elements? Write some code to figure this out. Then use the sort() algorithm that is part of the STL <algorithm> library. How does this function compare to the function you implemented?...
Please write C++ code Description: As a senior software engineer, I would like to determine and prove which sorting algorithm is the most efficient between quick sort, merge sort, and heap sort so that I can recommend it to the development team for inclusion in the code base of an upcoming code optimization project. Acceptance Criteria: Well documented, executable C++ source files including classes for each of the aforementioned sorting algorithms. In addition, each sorting algorithm class implementation should be...
URGENT ! PLEASE DO IN C# AND MAKE SURE I CAN COPY COPY CODE INTO VISUAL STUDIO Define a method named SortArray() to sort a one dimensional array of integers. YOU MAY NOT CALL A BUILT-IN SORTING FUNCTION, but should write your own. The method should return the sorted array. You may use whichever sorting algorithm you prefer, in order to sort the array. Please initialize an array and fill it with random values before beginning. Ensure that it has...
Use java code please not C++ or Python. Thank you. Write a recursive algorithm that counts the number of nodes in a linked list. Analyze the runtime of your algorithm
Write a C code for Modified Change problem: Apply the dynamic programming algorithm to find all the solutions to the change-making problem for the denominations 1, 3, 5 and the amount n = 9
[C++ language] Write down the steps how insertion sort for sorting the following elements in an array: 22, 1, 7, -9, 121, 75, 89, 29, 500, 43 I need Steps as an algorithm plus the code and screenshotes
please answer all parts and code thanks . - PART 1-Introduction to Sorting, 21 points Use this array of integer for the problems 1A, 1B, and 1c: 9 57 8324761 Each of these problems is worth 3 points A. Show the contents of the array each time a selection sort changes it while sorting the array into B. Show the contents of the array each time an insertion sort changes it while sorting C. Show the contents of the array...