Write a C++ Program to sort array of integers. Get the user input and display it in the sorted order.
#include<iostream>
#include<string>
using namespace std;
void sortIntegers(int values[],int n);
//The start point of the program
int main (){
//array of integers
int values[1000];
int n;
//Get the user input and display it in the sorted order.
cout<<"Enter the number of values: ";
cin>>n;
for(int i = 0; i<n; i++) {
cout<<"Enter the value ["<<(i+1)<<"]: ";
cin>>values[i];
}
//sort integers
sortIntegers(values,n);
//display integer values
cout<<"Integer values in the sorted order:\n";
for(int i = 0; i<n; i++) {
cout<<values[i]<<" ";
}
cout<<"\n\n";
system("pause");
return 0;
}
//This function "sortIntegers" takes array of integers and sorts integers values using a bubble sort.
void sortIntegers(int values[],int n){
for(int i = 0; i<n; i++) {
for(int j = i+1; j<n; j++)
{
if(values[j] < values[i]) {
int temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}
}
Comments
Leave a comment