c++ bubble sort

You are currently browsing articles tagged c++ bubble sort.

This is not pretty but it get’s the job done. It’s a fairly straightforward and (unnecessarily complicated and inefficient) example of a bubble sort used to sequentially sort an array. This array will be populated with pseudo random numbers using rand().

First, here is a header file (arraysort.h) with the necessary function prototype(s)/interface.

/*----- Greymask.com -----*/
#ifndef ARRAYSORT_H               // protect against multiple inclusion
#define ARRAYSORT_H

class Arraysort
{
private:
	int * array;            // pointer for array
	int size;
	bool sorted;            // set it from sort_array()

public:
	bool is_sorted(void);   // getter for sorted
	int  get_size(void);    // getter for size
	void load_array(int n); // load with random numbers mod n
	void print_array(void);
	void sort_array(void);  // bubble sort
	Arraysort(int n);       // Constructor
	~Arraysort(void);       // Destructor
};
#endif

Second, the meat of the program: Arraysort.cpp – This will contain all of our sorting and printing functions.

/*----- Greymask.com -----*/
#include <iostream>
#include <cstdlib>
#include "arraysort.h"

int *array;
int size;
bool sorted;

// constructor
Arraysort::Arraysort(int n)
{

    srand(time(0));		// seed random
    array = new int[n]; // allocate array dynamically
    size = n;           // set array size
    sorted = false;     // not sorted yet, so false
    load_array(n);      // load the array
}

int Arraysort::get_size()
{
    return size;        // return array size
}

void Arraysort::load_array(int n)
{
    sorted = false;
    size = n;
    int i = 0;

    do {
	array[i] = rand() % 100;	// fill array with random numbers
	i++;
    } while (i < size);
}

void Arraysort::print_array()
{
    // print array upto it's size
    int i = 0;
    do {
	std::cout << array[i] << std::endl;
	i++;
    } while (i < size);
}

void Arraysort::sort_array()
{
    // use bubble sort to sort the array from low to high
    int temp = 0;
    for (int j = 0; j < size - 1; j++) {
	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;
	    }
	}
    }
    // set sorted flag = true after sorting
    sorted = true;
}

bool Arraysort::is_sorted()
{
    return sorted;
}

// destructor
Arraysort::~Arraysort()
{
    delete[]array;
}

Finally, main.cpp to run the bad boy.

/*----- Greymask.com -----*/
#include <cstdio>
#include <cctype>
#include <iomanip>

#include <iostream>
using std::endl;
using std::endl;

#include "arraysort.h"

int main (int argc, char *argv[], char **env)
{
    Arraysort A(10);
	A.print_array();
	if (!A.is_sorted())
		A.sort_array();
	printf("==========================\n");
A.print_array();

return 0;

}

Here is how the output looks:

45
8
86
9
63
10
11
21
85
41
==========================
8
9
10
11
21
41
45
63
85
86

Again, this is not the best way to do this, but due to my slight dementia, I need to record this for future reference to get my nog going 🙂