//Sorting without using function:
#include<iostream>
using namespace std;
int main()
{
int size;
int arr[20], temp, loc, min;
cout << "Enter the size of array:";
cin >> size;
for (int i = 0; i < size; i++)
{
cout << "Enter the values of array:";
cin >> arr[i];
}
for (int i = 0; i < size - 1; i++)
{
min = arr[i];
loc = i;
for (int j = i + 1; j < size; j++)
{
if (arr[j] < min)
{
min = arr[j];
loc = j;
}
}
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
for (int i = 0; i < size; i++)
{
cout << "Array elements after selction is:";
cout << arr[i] << endl;
}
getchar();
getchar();
return 0;
}
//Sorting with using function:
#include<iostream>
using namespace std;
void values(int arr[20], int);
const int size = 5;
int main()
{
int arr[size];
for (int i = 0; i < size; i++)
{
cout << "Enter the value for " << 1 + i << ":";
cin >> arr[i];
}
values(arr, size);
getchar();
getchar();
return 0;
}
void values(int arr[], int size)
{
int min, loc, temp;
for (int i = 0; i < size - 1; i++)
{
min = arr[i];
loc = i;
for (int j = i + 1; j < size; j++)
{
if (arr[j] < min)
{
min = arr[j];
loc = j;
}
}
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
for (int i = 0; i < size; i++)
{
cout << "Values of array in accending order is:" << arr[i] << endl;
}
}
Comments
Post a Comment