(Exercise) Create-sortArray2.cpp In this part of the lab, you will write a progr
ID: 3668663 • Letter: #
Question
(Exercise) Create-sortArray2.cpp In this part of the lab, you will write a program that uses a different implementation of selection sort from the one you chose before: if you had chosen to search for the minimum for the traverse of the array, now choose the maximum, or . if you had chosen to search for the maximum for the traverse of the array, now choose the minimum. You may choose whether to traverse the array forward or backward. Also, this time you are requested to calculate the number of swaps used to complete sorting the array. You can write a program starting from the previous one, and modify it to sort the array using another selection sort implementation. Once the array is sorted, output first to the console if the output is sorted in ascending or descending order (depending on what you decided to use):Explanation / Answer
//sortArray1.cpp
//This is a C++ Program to Sort an Array using Selection Sort
// it will select minimum element every time
#include <iostream>
using namespace std;
void print (int [], int);
void selection_sort (int [], int);
//Driver Function
int main ()
{
int ar[] = {10, 2, 45, 18, 16, 30, 29, 1, 1, 100};
cout << "Array initially : ";
print (ar, 10);
selection_sort (ar, 10);
cout << "Array after selection sort : ";
print (ar, 10);
return 0;
}
// Selection Sort
void selection_sort (int ar[], int size)
{
int min_ele_loc;
for (int i = 0; i < 9; ++i)
{
//Find minimum element in the unsorted array
//Assume it's the first element
min_ele_loc = i;
//Loop through the array to find it
for (int j = i + 1; j < 10; ++j)
{
if (ar[j] < ar[min_ele_loc])
{
//Found new minimum position, if present
min_ele_loc = j;
}
}
//Swap the values
swap (ar[i], ar[min_ele_loc]);
}
}
//Print the array
void print (int temp_ar[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << temp_ar[i] << " ";
}
cout << endl;
}
//sortArray2.cpp
//This is a C++ Program to Sort an Array using Selection Sort
// it will select maximum element every time
#include <iostream>
using namespace std;
void print (int [], int);
void selection_sort (int [], int);
//Driver Function
int main ()
{
int ar[] = {10, 2, 45, 18, 16, 30, 29, 1, 1, 100};
cout << "Array initially : ";
print (ar, 10);
selection_sort (ar, 10);
cout << "Array after selection sort : ";
print (ar, 10);
return 0;
}
// Selection Sort
void selection_sort (int ar[], int size)
{
int max_ele_loc;
int no_of_swap = 0;
for (int i = size-1; i > 0; --i)
{
//Find maximum element in the unsorted array
//Assume it's the last element
max_ele_loc = i;
//Loop through the array to find it
for (int j = i - 1; j >= 0; --j)
{
if (ar[j] > ar[max_ele_loc])
{
//Found new maximum position, if present
max_ele_loc = j;
}
}
if(max_ele_loc != i){
swap (ar[i], ar[max_ele_loc]);
no_of_swap++;
}
}
cout<<"Number of swap: "<<no_of_swap<<endl;
}
//Print the array
void print (int temp_ar[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << temp_ar[i] << " ";
}
cout << endl;
}
//searchArray.cpp
/*
* C++ Program to Implement Linear Search Algorithm
*/
#include <iostream>
using namespace std;
/* Linear Search Function */
int lin_search(int arr[], int size, int val)
{
int key = -1;
for (int i = 0; i < size; i++)
{
if (arr[i] == val)
{
key = i;
break;
}
}
return key;
}
int main()
{
int num, val,key;
cout << "Enter the number of elements in array: ";
cin >> num;
int arr[num];
for (int i = 0; i < num; i++)
{
cin >> val;
arr[i] = val;
}
cout<<"Enter the number to be search: ";
cin>>val;
key = lin_search(arr, num, val);
if (key != -1)
cout << " Element " << val
<< " is at position " << ++key;
else
cout << " Element " << val
<< " is not present";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.