Random array sorting..
Dec 7, 2016 at 5:31am UTC
Hi,
I would like to make a random array in 4*5 first.
Then supply selection and bubble sort to fix the
same number in two separate lines. (show the
numbers in one line first.)
Thank for help
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
#include<iostream>
#include<iomanip>
#include<ctime>
#include <cstdlib>
using namespace std;
int main()
{
int x = 4, y = 5,a;
int **ptr = new int *[x];
int i,j,n,temp1,temp2;
for (int i = 0; i<y; i++)
{
ptr[i] = new int [y];
}
for (int j = 0; j<x; j++)
{
for (int k = 0; k<y; k++)
{
a =( rand() % 100 )+ 1;
ptr[j][k] = a;
cout<<ptr[j][k]<<" " ;
}
for (int k = 1; k<j; k++)
{
a = rand() % 100 + 1;
ptr[j][k] = a;
cout<<ptr[j][k];
}
cout <<endl;
}
}
int ptr[20],j,a;
for (int j = 0; j< 19; j++)
{
a = rand() % 100 + 1;
ptr[20]=a;
cout<<ptr[20]<<" " ;
}
cout<<"\nArray after selection sort: " <<temp1;
cout<<"\nArray after bubble sort: " <<temp2;
{
for (j=0;j<n;j++); //selection sort
for (j=i+1;j<n;j++)
if (ptr[j]<ptr[i])
i=j;
if (i!=i)
{
temp1=ptr[j];
ptr[j]=ptr[j+1];
ptr[j+1]=temp1;
}
}
{
for (i = 0; i < n ; i++); //bubble sort
for (j=0;j<(n-i);++j)
if (ptr[j]>ptr[j+1])
{
temp2=ptr[j];
ptr[j]=ptr[j+1];
ptr[j+1]=temp2;
}
}
Topic archived. No new replies allowed.