As you have seen in lecture, it is often necessary to sort an array in order to
ID: 3668488 • Letter: A
Question
As you have seen in lecture, it is often necessary to sort an array in order to make searching faster. In this part of the lab, you will implement selection sort. The selection sort algorithm consists of traversing the collection of unsorted elements to find the maximum (or minimum) value so far, and to swap it with the last one that is unsorted, and then to repeat the process. There are multiple ways to implement the selection sort algorithm, in addition to deciding whether to sort the array in an ascending versus descending order. More specifically, you may choose to:Explanation / Answer
#include <iostream>
using namespace std;
void print (int [], int);
void selection_sort (int [], int);
//Driver Function
int main ()
{
int min_ele_loc;
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.