1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <iostream>
using namespace std;
bool isSorted(int const numbers[], int low, int high);
void swap(int numbers[], int i, int j);
void sort(int numbers[], int low, int high);
void display(const int numbers[], int low, int high);
int getindexoflargest(int const numbers[], int low, int high);
int main()
{
int low;
int high;
int i;
int numbers[10];
cout<<"Enter 10 numbers:\n";
for(i=0; i<10; i++) {
cin>>numbers[i];
}
cout<<"Enter lower and upper bound:\n";
cin>>low;
cin>>high;
if (isSorted(numbers,low,high)) {
cout<<"Range is sorted.\n";
}
else
{
cout<<"Range is not sorted.\n";
}
cout<<"After sorting:\n";
sort(numbers,low, high);
display(numbers, 0, 9);
return 0;
}
bool isSorted(int const numbers[], int low, int high)
{
int yes=0;
int range;
range=high-low;
for (int i=low; i<high; i++) {
if (numbers[i]<=numbers[i+1])
{
yes=yes+1;
}
}
if (yes==range) {
return true;
}
else
return false;
}
int getindexoflargest(int const numbers[], int low, int high) {
int index=high;
int i;
for (i=low + 1; i <= high; i = i+1) {
if (numbers[i] > numbers[index]) {
index=i;
}
}
return index;
}
void swap(int numbers[], int i, int j) {
int temp=numbers[i];
numbers[i]=numbers[j];
numbers[j]=temp;
return;
}
void display(const int numbers[], int low, int high) {
int i;
for (i=low; i<=high; i++)
{
cout<<numbers[i]<<"\n";
}
return;
}
void sort(int numbers[], int low, int high) {
int i;
int index;
for (i=low; i <= high; i++) {
index=getindexoflargest(numbers, low, high);
swap(numbers, i, index);
}
}
|