Sorting code

May 5, 2016 at 1:29am
Our teacher wants us to show the "number of passes" or the difference in passes in the Selection, Insertion, Bubble, Merge, Quicksort, Radix sorting. Cause the output is all the same--the one she wants to see is HOW it differs in the passes. Could you guys help me out?


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

using namespace std;


template <class T> //SELECTION SORTING

void s_sort(T a[],int n)
{
int i,j,t;

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i]) //for descending order use if(a[j]>a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}


int main()
{
int a[100],i,n;

cout<<"Enter The number of Element:\n";
cin>>n;
cout<<"\nEnter Elements:\n";

for(i=0;i<n;i++)
{

cout<<"\nEnter:";
cin>>a[i];
}
s_sort(a,n);
cout<<"\nAfter Sorting\n";

for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}

getch();
return 0;
}
May 5, 2016 at 7:32am
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
65
66
#include <iostream>

template < typename T > void sort( T a[], int n, int& num_cmp, int& num_swap )
{
    num_cmp = num_swap = 0 ;

    for( int i = 0 ; i < n ; ++i )
    {
        for( int j = i+1 ; j < n ; ++j )
        {
            ++num_cmp ;
            if( a[j] < a[i] )
            {
                ++num_swap ;
                const T temp = a[i] ;
                a[i] = a[j] ;
                a[j] = temp ;
            }
        }
    }
}

template < typename T > void print( const T a[], int n )
{
    std::cout << "[ " ;
    for( int i = 0 ; i < n ; ++i ) std::cout << a[i] << ' ' ;
    std::cout << "] (size:" << n << ")" ;
}

template < int SZ > void test()
{
    int num_cmp = 0 ;
    int num_swap = 0 ;

    int seq[SZ] = {} ; // all zeroes
    std::cout << "[ ... ] (size:" << SZ << ")" ;
    sort( seq, SZ, num_cmp, num_swap ) ;
    std::cout << "    #compares: " << num_cmp << '\n' ;
}

int main()
{
    const int n = 10 ;
    int num_cmp = 0 ;
    int num_swap = 0 ;

    int a[n] = { 7, 10, 23, 32, 45, 56, 62, 75, 83, 94 } ;
    print( a, n ) ;
    sort( a, n, num_cmp, num_swap ) ;
    std::cout << "    #compares: " << num_cmp << "   #swaps: " << num_swap << "\n\n" ;

    int b[n] = { 90, 81, 72, 63, 54, 45, 36, 27, 18, 9 } ;
    print( b, n ) ;
    sort( b, n, num_cmp, num_swap ) ;
    std::cout << "    #compares: " << num_cmp << "   #swaps: " << num_swap << "\n\n" ;

    int c[n] = { 81, 18, 9, 72, 36, 54, 45, 90, 27, 63 } ;
    print( c, n ) ;
    sort( c, n, num_cmp, num_swap ) ;
    std::cout << "    #compares: " << num_cmp << "   #swaps: " << num_swap << "\n\n" ;

    test<50>() ;
    test<250>() ;
    test<1000>() ;
    test<30'000>() ;
} 

1
2
3
4
5
6
7
8
9
10
[ 7 10 23 32 45 56 62 75 83 94 ] (size:10)    #compares: 45   #swaps: 0

[ 90 81 72 63 54 45 36 27 18 9 ] (size:10)    #compares: 45   #swaps: 45

[ 81 18 9 72 36 54 45 90 27 63 ] (size:10)    #compares: 45   #swaps: 20

[ ... ] (size:50)    #compares: 1225
[ ... ] (size:250)    #compares: 31125
[ ... ] (size:1000)    #compares: 499500
[ ... ] (size:30000)    #compares: 449985000 

http://coliru.stacked-crooked.com/a/c0d865517cd9c311
May 8, 2016 at 3:47am
This works perfectly! But it has to be us to input the variables, it shouldn't be defined in the code because in your main program, there already is a pre-defined set, it should be the user who will input his own set. Try to run my program.
May 8, 2016 at 5:47am
> it should be the user who will input his own set.

You already know how to accept numbers entered by the user into an array.
So, go ahead and write the code for that.
May 8, 2016 at 7:06am
Oh right, forgot about that. Thanks a lot for the help!
Topic archived. No new replies allowed.