0% found this document useful (0 votes)
34 views10 pages

#Include #Include #Include Void Mergesort (Int, Int, Int ) Void Ascending (Int )

The document contains 3 code snippets related to sorting arrays in C++. The first snippet sorts two integer arrays into ascending order and then merges them into a third array. The second snippet sorts a 2D integer array both by row and column using selection sort. The third snippet uses bubble sort to first sort a 2D character array and its corresponding integer array alphabetically, and then to sort the integer array in descending order based on vote counts.

Uploaded by

Usama Javed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views10 pages

#Include #Include #Include Void Mergesort (Int, Int, Int ) Void Ascending (Int )

The document contains 3 code snippets related to sorting arrays in C++. The first snippet sorts two integer arrays into ascending order and then merges them into a third array. The second snippet sorts a 2D integer array both by row and column using selection sort. The third snippet uses bubble sort to first sort a 2D character array and its corresponding integer array alphabetically, and then to sort the integer array in descending order based on vote counts.

Uploaded by

Usama Javed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Q#1:

#include<iostream.h>
#include<time.h>
#include<conio.h>
void mergesort(int [],int [],int []);
void ascending(int[]);

void show(int [], int);


void insert(int [], int);
void main()
{int arr1[10];
int arr2[10];
int arr3[20];
insert(arr1,10);
insert(arr2,10);
cout<<"arranging frst array"<<endl;
ascending(arr1);
cout<<"arranging 2nd array"<<endl;
ascending(arr2);
cout<<"showing frst array"<<endl;
show(arr1,10);
cout<<"showing 2nd array"<<endl;
show(arr2,10);
cout<<"merging array 1 and 2 in array 3"<<endl;
mergesort(arr1,arr2,arr3);
cout<<"showing merge sorted array"<<endl;
show(arr3,20);
getche();
}
void ascending(int x[10])
{int temp=0;
for(int i=0;i<10;i++)

for(int j=0; j<10; j++)


{
{if(x[i]<x[j])
{temp=x[i];
x[i]=x[j];
x[j]=temp;
}}
}
}

void mergesort(int a[10], int b[10], int c[20])


{int temp;
for (int i=0; i<20; i++)
{if(a[i/2]<b[i/2])
{c[i]=a[i/2];
c[i+1]=b[i/2];}
else
{c[i]=b[i/2];
c[i+1]=a[i/2];}

}
}
void show(int arr[], int n)
{for(int i=0; i<n; i++)
{cout<<arr[i]<<endl; }

}
void insert(int x[],int n)
{for (int i=0;i<n;i++)
cin>>x[i];
}

Q#2:
#include <iostream.h>
#include <conio.h>
#define m 3
#define n 3
void rowsort(int a[][n])
{int i,j,k;
for (k=0;k<n;k++)
{
for (i=1;i<m;i++)
{int key=a[k][i];
for (j=i-1;j>=0 && a[k][j]>key;j--)
{

a[k][j+1]=key;
}
}
for (i=0;i<m; i++)
{for (j=0;j<n;j++)

a[k][j+1]=a[k][j];}

cout<<a[i][j]<<'\t';
cout<<endl;}
}
void colsort(int a[][n])
{int i,j,k;
for (k=0;k<m;k++)
{
for (i=1;i<m;i++)
{int key=a[i][k];
for (j=i-1;j>=0 && a[j][k]>key;j--)
{a[j+1][k]=a[j][k];}
a[j+1][k]=key;
}
}
for (i=0;i<m; i++)
{for (j=0;j<n;j++)
cout<<a[i][j]<<'\t';
cout<<endl;}

void main ()
{int a[m][n];
int i,j,k;
for (i=0;i<m; i++)

{for (j=0;j<n;j++)
cin>>a[i][j];}
cout<<endl;
for (i=0;i<m; i++)
{for (j=0;j<n;j++)
cout<<a[i][j]<<'\t';
cout<<endl;}
cout<<endl;
rowsort(a);
cout<<endl;
colsort(a);

getche();
}

Q#3:
Part 1: (Alphabetic order using bubble sort)
#include <iostream.h>
#include <conio.h>
#define m 3
#define n 3
void alfa(char a[][n], int b[] [n])

{int i,j,k;
int jj,ii;
int t1[n];
char t2[n];
for (int p=0; p<n; p++)
{for (i=0; i<(m-1); i++)
{for (j=0; j<n; j++)
{if (a[i][j]>a[i+1][j])
{for (k=0; k<n; k++)
{t2[k]=a[i][k];
t1[k]=b[i][k];}
for (k=0; k<n; k++)
{a[i][k]=a[i+1][k];
b[i][k]=b[i+1][k];}
for (k=0; k<n; k++)
{a[i+1][k]=t2[k];
b[i+1][k]=t1[k];}}}}}
for (i=0; i<m; i++)
{for (j=0; j<n; j++)
cout<<a[i][j];
cout<<endl;}
cout<<endl;
for (i=0; i<m; i++)
{for (j=0; j<n; j++)
cout<<b[i][j];

cout<<endl;}
}

void main ()
{char a[m][n];
int b[m][n];
int i,j;
cout<<"enter names of candidates:"<<endl;
for (i=0; i<m; i++)
{for (j=0; j<n; j++)
cin>>a[i][j];}
cout<<"enter no of votes:"<<endl;
for (i=0; i<m; i++)
{for (j=0; j<n; j++)
cin>>b[i][j];}
for (i=0; i<m; i++)
{for (j=0; j<n; j++)
cout<<a[i][j];
cout<<endl;}
cout<<endl;
for (i=0; i<m; i++)
{for (j=0; j<n; j++)
cout<<b[i][j];
cout<<endl;}
alfa(a,b);

getche();}

Part2: (descending order of votes):


#include<iostream.h>
#include<conio.h>
void reset(int votes[][5], char names[][5]);
void main()
{int votes[5][5];
char names[5][5];
for(int i=0;i<3;i++)
{for(int j=0;j<4;j++)
{cin>>votes[i][j];}}

for(int i=0;i<5;i++)
{for(int j=0;j<5;j++)
{cin>>names[i][j];}}
reset(votes,names);
getche();

}
void reset(int votes[5][5], char names[5][5])
{int tempvotes[5];
char tempnames[5];
int x=votes[0][3];
for(int i=0; i<5;i++)
{for(int j=0; j<5;j++)

{
if(votes[i][3]<votes[i][j])
{int temp= votes[i][3];
votes[i][3]=votes[i][j];
for(int k=0; k<5;k++)
{tempvotes[k]=votes[i][k];
tempnames[k]=names[i][k];}

}
for(int l=0; l<5;l++)
{votes[i][l]=votes[j][l];
names[i][l]=votes[j][l];
}
for(int m=0; m<5;m++)
{votes[j][m]=tempvotes[m];
names[j][m]=tempnames[m];
}
}}
}

You might also like