PropellerAds

Showing posts with label sorting. Show all posts

Program, code to sort array in ascending or descending order in C++

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;
}
}

how to sort a 2d array in C++

This program demonstrate how to sort 2d array in ascending order. The program explain it in 3x3 array you could change the values as required to meet your requirement

//prog for 2d array sort

#include<iostream>
#include<conio.h>

using namespace std;

void main() {
int arr[3][3];
int temp;

cout << "Enter 9 elements\n\n";

//Enter Element
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << "Enter Elements no: " << i+j+1 << " : ";
cin >> arr[i][j];
}
}
cout <<  endl;

//print array
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << arr[i][j] << "\t" ;
}
cout <<  endl;
}

// run the loop double times the number of row or column which ever is greater
for (int k = 0; k <6; k++){
//arrage elemnt in ascending order in a rows
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++){
if(arr[i][j] > arr[i+1][j]){
temp = arr[i][j];
arr[i][j] = arr[i+1][j];
arr[i+1][j] = temp;
}
}

//compare the last element of each row with first element of its next row
for (int i = 0; i < 2; i++){
if(arr[2][i] > arr[0][i+1]){
temp = arr[2][i];
arr[2][i]  = arr[0][i+1];
arr[0][i+1] = temp;
}
}
}



}
cout <<  endl << endl;

// print array
for(int j = 0; j < 3; j++){
for(int i = 0; i < 3; i++){
cout << arr[i][j] << "\t" ;
}
cout <<  endl;
}
_getch();
}



This program arrange the rows value in in itself and then compare the last value of a row with the first value of the next row. And if it is greater it swaps them and again  arrange the the row. The program continue to do it unless all the value get sorted. Go through the program and you will get it how it works
Monday, 22 December 2014
Posted by Unknown
PropellerAds
PropellerAds

Popular Post

Powered by Blogger.

- Copyright © BSCS -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -