c++, data structure
Write a solution to test 2 problem 3 that uses selection sort to sort the items in the vector of integers.
#include <iostream>
#include <iterator>
#include <string>
#include <random>
using namespace std;
void insertionSort(int a[], int N)
{
int sorted = 0;
for (int sorted = 0; sorted < N - 1;
++sorted)
{
int item_position = sorted +
1;
int item = a[item_position];
while (item_position > 0
&& a[item_position - 1] > item)
{
a[item_position]
= a[item_position - 1];
--item_position;
copy(a, a + N,
ostream_iterator<int>(cout, " ")); cout << endl;
}
a[item_position] = item;
copy(a, a + N,
ostream_iterator<int>(cout, " ")); cout << endl
<< endl;
}
}
int main()
{
default_random_engine e(20190327);
uniform_int_distribution<int> usize(16,
32);
uniform_int_distribution<int> uelts(10,
99);
int N = usize(e);
N = 24;
cout << N << endl;
int *elts = new int[N];
for (int i = 0; i < N; ++i)
elts[i] = uelts(e);
copy(elts, elts + N,
ostream_iterator<int>(cout, " ")); cout << endl;
insertionSort(elts, N);
system("pause");
return 0;
}
#include <iostream>
#include <iterator>
#include <string>
#include <random>
#include <vector>
using namespace std;
void selectionSort(vector<int> array, int arraySize){
for (int iterationNum = 0; iterationNum < arraySize-1;
iterationNum++){
int minIndex = iterationNum;
for (int j = iterationNum+1; j < arraySize; j++){
if (array[j] < array[minIndex])
minIndex = j;
}
// swap array[minIndex] with array[iterationNum]
int temp = array[minIndex];
array[minIndex] = array[iterationNum];
array[iterationNum] = temp;
copy(array.begin(), array.end(), ostream_iterator<int>(cout,
" ")); cout << endl;
cout<<endl;
}
}
int main()
{
default_random_engine e(20190327);
uniform_int_distribution<int> usize(16, 32);
uniform_int_distribution<int> uelts(10, 99);
int N = usize(e);
N = 24;
cout << N << endl;
vector<int>elts;
for (int i = 0; i < N; ++i){
elts.push_back(uelts(e));
}
copy(elts.begin(), elts.end(), ostream_iterator<int>(cout,
" ")); cout << endl;
selectionSort(elts, N);
system("pause");
return 0;
}
============================================
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any concern.
C++, data structure Write a solution to test 2 problem 3 that uses selection sort to sort the ite...
This is a c++ code /** Write a function sort that sorts the elements of a std::list without using std::list::sort or std::vector. Instead use list<int>::iterator(s) */ using namespace std; #include <iostream> #include <iomanip> #include <string> #include <list> void print_forward (list<int>& l) { // Print forward for (auto value : l) { cout << value << ' '; } cout << endl; } void sort(list<int>& l) { write code here -- do not use the built-in list sort function }...
Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...
//CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm. #include <iostream> using std::cout; using std::endl; template<class T> void sort(T a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //The assignment and < operator work for values of type T. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <=... <= a[numberUsed -...
C++ problem. Please just find and fix the errors and highlight it afterwards. Do not add any new comments or remove comments from teachers. Thank you very much *R 2B PROGRAM 1B: INSERTION SORT Find and rix errors. Ron the program once and save the outpat as a conment at the end of the source file Changed by: IDE #include <iostream> using namespace std: void insertionSort (int aryll, int size) int main double list(1001-(50.1, 30.2, 80.3, 10.5, 30.2, 40.9, 90.8,...
I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...
Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) { int i, j, k, c[100000]; i = low; k = low; j = mid + 1; while (i <= mid && j <= high) { if (a[i] < a[j]) { c[k] = a[i]; k++; i++; } else { ...
How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't. Here's my code. #include <iostream> using namespace std; void selectionSort(int[], int, int& ); void showSelection(int[], int); void sortArray(int[], int, int&); void showArray(const int[], int); int main() { int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24...
Change the the following C++ codes to list the "marks" reported Alphabetically. --------------------------------------------------------------------------------------------------------- #include <iostream> #include<string.h> using namespace std; void display(int marks[10]); int main() { int marks[10]; int i;x for (i=0; i<10; i++) { cout << "Enter marks : "; cin >> marks[i]; } display(marks); return 0; } // Create the function sort to sort the marks in alphabetical order void display(int marks[10]) { cout <<"Displaying marks in Ascending Order " << endl; for (int i = 0; i <10;...