//Sorting without using functions:
#include<iostream>
using namespace std;
int main()
{
int size;
int arr[20], temp, swap;
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 = 1; i < size; i++)
{
swap = 0;
for (int j = 0; j < (size-i); j++)
{
if (arr[j] < arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j + 1] = temp;
swap = 1;
}
}
if (swap == 0)
{
break;
}
}
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 " << i + 0 << ":";
cin >> arr[i];
}
values(arr, size);
getchar();
getchar();
return 0;
}
void values(int arr[], int size)
{
int temp, swap;
for (int i = 1; i < size; i++)
{
swap = 0;
for (int j = 0; j < size - i; j++)
{
if (arr[j] < arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swap = 1;
}
}
if (swap == 0)
break;
}
for (int i = 0; i < size; i++)
{
cout << "Values in assending order are " << arr[i] << endl;
}
}
Comments
Post a Comment