Random Number Bubble Sort

Hi, I am new to this still getting the hang of the language.
I am trying to write a code that uses the bubble method to sort 20 randomly generated number (numbers can be between 1 and 100). I would like to first print out the number before they are sorted, then the number after being sorted by the bubble method. I have been fiddle with the code for a bit not sure how to fix...

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
67
68
69
70
71
72
73
74
75
76
#include <cstdlib> 
#include <ctime> 
#include <iostream>
#include <math.h>

using namespace std;

void sortedArray(int [], int);
void unsortedArray(int [], int);

int main() 
{   
    
 int size = 20;  //for dynamic allocation of array size
 
 int *array = new int[size]; // dynamically allocating the value 20 to the array size 
 
 srand((unsigned)time(0)); 
  
   for(int i=0; i<size; i++)
  
   { 
        
	    array[i] = (rand()%100)+1; // random numbers between 1 and 100 choose here 
	   
	   cout<<"Values of unsorted array"<<endl;
       cout<<array[i]<<endl;
       unsortedArray(array, size); 
       
       // here begins the sorting using the bubble method	    
       cout<<"Sorted Values using Bubble Method"<<endl;
       cout<<array[i]<<endl;
       sortedArray(array, size);
   }

   	    
   
   

	    //unsortedArray(array,20);
	    
	    delete [] array;
return 0;
   }
   
 void sortedArray(int array[], int size)
 {
   bool swap;      
   int temp; 
   do    
    { 
       swap = false;
       for (int i = 0; i < (size - 1); i++)
       { 
          if (array[i] > array[i + 1])
          {
             temp = array[i];
             array[i] = array[i + 1]; 
             array[i + 1] = temp;
             swap = true;  
          } 
        } 


     } while (swap); 

}

void unsortedArray(const int array[], int size)
{
	for (int i = 0; i <size; i++)
	
		cout<<array[i]<<""<<cout<<endl;;
	
	
}
Last edited on
couple of issues straight off:
- function signatures for unsortedArray() don't match b/w declaration and definition (your compiler would have mentioned it as well)
- you're calling unsortedArray(), sortedArray() after each iteration - these functions should be called just once each - unsortedArray() after the array has been filled with random numbers and sortedArray() after the array has been sorted

another point - for random number generation prefer using the tools from <random> header file rather than cstdlib, etc (various discussions on this forum recent past why so):
http://www.cplusplus.com/forum/beginner/210040/#msg986693
Any suggestion on how to improve the code, I got it working

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
67
68
69
70
71
72
73
74
75
#include <cstdlib> 
#include <ctime> 
#include <iostream>
#include <math.h>

using namespace std;

int sortedArray(int [], int);
int unsortedArray(int [], int);

int main() 
{   
    
 int size = 20;  //for dynamic allocation of array size
 
 int *array = new int[size]; // dynamically allocating the value 20 to the array size 
 
 srand((unsigned)time(0)); 
  
   for(int i=0; i<size; i++)
  
   { 
        
	    array[i] = (rand()%100)+1; // random numbers between 1 and 100 choose here 
	   
	 //  cout<<"Values of unsorted array"<<endl;
       //cout<<array[i]<<endl;
      // unsortedArray(array, size); 
       
       // here begins the sorting using the bubble method	    
       //cout<<"Sorted Values using Bubble Method"<<endl;
       //cout<<array[i]<<endl;
       //sortedArray(array, size);
   }

   	    
   
   

	    //unsortedArray(array,20);
	    
	    delete [] array;
return 0;
   }

int unsortedArray(const int array[], int size)
{
	for (int i = 0; i <size; i++)
	
		cout<<array[i]<<""<<cout<<endl;
return array[i];	
}   

 int sortedArray(int array[], int size) 
 {
   bool swap;      
   int temp; 
   do    
    { 
       swap = false;
       for (int i = 0; i < (size - 1); i++)
       { 
          if (array[i] > array[i + 1])
          {
             temp = array[i];
             array[i] = array[i + 1]; 
             array[i + 1] = temp;
             swap = true;  
          } 
        } 


     } while (swap); 

}
Last edited on
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 <cstdlib>
#include <ctime>
#include <iostream>

void print_array( const int array[], int size ) ;
void sort_array( int array[], int size ) ;

int main()
{
    const int size = 20 ; // size is known at compile time
    const int max_v = 100 ;

    int array[size] {} ; // initialised to all zeroes (no need for dynamic allocation)
    std::srand( std::time(nullptr) );
    for( int& v : array ) v = std::rand() % max_v + 1 ;

    std::cout << "unsorted array:\n\t" ;
    print_array( array, size ) ;

    sort_array( array, size ) ;
    std::cout << "sorted array:\n\t" ;
    print_array( array, size ) ;
}

void print_array( const int array[], int size )
{
    if( array == nullptr ) return ;

    for( int i = 0 ; i < size ; ++i ) std::cout << array[i] << ' ' ;
    std::cout << '\n' ;
}

void swap_items( int array[], int a, int b ) // invariant: valid array, positions a, b
{
    const auto temp = array[a] ;
    array[a] = array[b] ;
    array[b] = temp ;
}

// return true if there was a swap
bool bubble_up( int array[], int size ) // invariant: valid array, size > 1
{
    bool swapped = false ;

    for( int i = 0; i < (size-1); ++i )
    {
        if( array[i] > array[i+1] )
        {
            swap_items( array, i, i+1 ) ;
            swapped = true ;
        }
    }

    return swapped ;
}

void sort_array( int array[], int size )
{

    if( array == nullptr || size < 2 ) return ;

    // keep bubbling up till there are no more swaps
    while( bubble_up( array, size ) ) ;
}

http://coliru.stacked-crooked.com/a/84eab6934af1a96e
Topic archived. No new replies allowed.