Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int a[10],b[10],size;
- void swap(int *xp, int *yp)
- {
- int temp = *xp;
- *xp = *yp;
- *yp = temp;
- }
- void input(int array[], int size)
- {
- for (int i = 0; i < size; i++)
- {
- array[i]=rand()%7;
- }
- }
- void Traverse(int array[], int size)
- {
- for (int i = 0; i < size; i++)
- {
- cout << array[i] << " ";
- }
- cout << endl;
- }
- void InsertionSort(int array[], int size)
- {
- for (int a = 1; a < size; a++)
- {
- int key = array[a];
- int j = a - 1;
- while (key < array[j] && j >= 0)
- {
- array[j + 1] = array[j];
- j--;
- }
- array[j + 1] = key;
- }
- }
- void selectionSort(int array[],int size )
- {
- int i, j, min;
- for (i = 0; i < size-1; i++)
- {
- min = i;
- for (j = i+1; j < size; j++)
- if (b[j] < b[min])
- min = j;
- swap(&b[min], &b[i]);
- }
- }
- int main()
- {
- /*cout<<"Size:";
- cin>>size;
- for(int i=0; i<size; i++)
- {
- a[i]=rand()%5;
- }
- for(int i=0; i<size; i++)
- {
- b[i]=rand()%6;
- }*/
- int p;
- cout<<"1 for insertion sort\n2 for selection sort\n0 for termination:"<<endl;
- while(1)
- {
- cout<<"Press:";
- cin>>p;
- if(p==1)
- {
- cout<<"Size:";
- cin>>size;
- input(a,size);
- Traverse(a,size);
- InsertionSort(a,size);
- Traverse(a,size);
- }
- else if(p==2)
- {
- cout<<"Size:";
- cin>>size;
- input(b,size);
- Traverse(b,size);
- selectionSort(b,size);
- Traverse(b,size);
- }
- else
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement