- Back to Home »
- algo , algorithm , ascending , c++ , code , coding , descending , program , project , project c++(basic) , sort , sorting »
- Program, code to sort array in ascending or descending order in C++
Posted by : Unknown
Wednesday, 24 December 2014
This program demonstrate how to sort a array in ascending or descending order.. in C++.
This program uses function to do this and call the function of ascending or decending function whichever may user selects.
In Ascending algo
The program uses swap algorithm in which 1st and 2nd digits are compared if 2nd is smaller the program swaps them and then compare 2nd and 3rd digit... and continue to do so till the end of array in this manner the greatest digit reaches at end position. This loops is again repeated n-1 times so that all digits attain their respective position.....
#include<iostream>
#include<conio.h>
using namespace std;
const int array_size = 10;
void data(int arr[]) {
char ch[2] = {'s', 't'};
for(int i = 0; i<array_size; i++){
cout << "Please Enter " << i+1 << ch[0] << ch[1] << " Number: ";
cin >> arr[i];
if(i == 0){ch[0] = 'n'; ch[1] = 'd';}
if(i == 1){ch[0] = 'r'; ch[1] = 'd';}
if(i >= 2){ch[0] = 't'; ch[1] = 'h';}
}
cout << endl;
}
void asscending(int arr[]) {
for(int j = 0; j<(array_size-1); j++){
for(int i = 0; i<(array_size-1); i++){
int a;
if(arr[i]>(arr[i+1])){
a = arr[i];
arr[i] = arr[i+1];
arr[i+1] = a;
}
}
}
cout << endl;
}
void descending(int arr[]) {
for(int j = 0; j<(array_size-1); j++){
for(int i = 0; i<(array_size-1); i++){
int a;
if(arr[i]<arr[i+1]){
a = arr[i];
arr[i] = arr[i+1];
arr[i+1] = a;
}
}
}
cout << endl;
}
void print(int arr[]) {
for (int i = 0; i <array_size; i++){
cout << arr[i] << endl;
}
cout << endl;
}
void cases(int arr[]) {
char sort;
cout << "Press a to sort in ascending and b to sort in desending order: ";
cin >> sort;
switch (sort){
case 'A':
case 'a':
asscending(arr);
cout << "The asending order is:" << endl;
print(arr);
break;
case 'B':
case 'b':
descending(arr);
cout << "The desending order is:" << endl;
print(arr);
break;
default:
cout << "wrong input";
cases(arr);
break;
}
cout << endl;
}
void main() {
int arr[array_size] = {0};
char a = 'a';
while(a != 'c'){
if(a == 'a' || a == 'A'){
data(arr);
cases(arr);
}
if(a == 'b' || a == 'B'){
cases(arr);
}
cout << "Press a to renter data press b to rearrange same data, c to terminate: ";
cin >> a;
}
}
This program uses function to do this and call the function of ascending or decending function whichever may user selects.
In Ascending algo
The program uses swap algorithm in which 1st and 2nd digits are compared if 2nd is smaller the program swaps them and then compare 2nd and 3rd digit... and continue to do so till the end of array in this manner the greatest digit reaches at end position. This loops is again repeated n-1 times so that all digits attain their respective position.....
#include<iostream>
#include<conio.h>
using namespace std;
const int array_size = 10;
void data(int arr[]) {
char ch[2] = {'s', 't'};
for(int i = 0; i<array_size; i++){
cout << "Please Enter " << i+1 << ch[0] << ch[1] << " Number: ";
cin >> arr[i];
if(i == 0){ch[0] = 'n'; ch[1] = 'd';}
if(i == 1){ch[0] = 'r'; ch[1] = 'd';}
if(i >= 2){ch[0] = 't'; ch[1] = 'h';}
}
cout << endl;
}
void asscending(int arr[]) {
for(int j = 0; j<(array_size-1); j++){
for(int i = 0; i<(array_size-1); i++){
int a;
if(arr[i]>(arr[i+1])){
a = arr[i];
arr[i] = arr[i+1];
arr[i+1] = a;
}
}
}
cout << endl;
}
void descending(int arr[]) {
for(int j = 0; j<(array_size-1); j++){
for(int i = 0; i<(array_size-1); i++){
int a;
if(arr[i]<arr[i+1]){
a = arr[i];
arr[i] = arr[i+1];
arr[i+1] = a;
}
}
}
cout << endl;
}
void print(int arr[]) {
for (int i = 0; i <array_size; i++){
cout << arr[i] << endl;
}
cout << endl;
}
void cases(int arr[]) {
char sort;
cout << "Press a to sort in ascending and b to sort in desending order: ";
cin >> sort;
switch (sort){
case 'A':
case 'a':
asscending(arr);
cout << "The asending order is:" << endl;
print(arr);
break;
case 'B':
case 'b':
descending(arr);
cout << "The desending order is:" << endl;
print(arr);
break;
default:
cout << "wrong input";
cases(arr);
break;
}
cout << endl;
}
void main() {
int arr[array_size] = {0};
char a = 'a';
while(a != 'c'){
if(a == 'a' || a == 'A'){
data(arr);
cases(arr);
}
if(a == 'b' || a == 'B'){
cases(arr);
}
cout << "Press a to renter data press b to rearrange same data, c to terminate: ";
cin >> a;
}
}