HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Sorting algorithms implemented in C++

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sortingimplementedalgorithms

Problem

Implemented
Bubble Sort, Insertion Sort, Selection Sort,
Quick Sort, Merge Sort and
Radix Sort

https://code.google.com/p/medicine-cpp/source/browse/trunk/cpp/sorting/SortingAlgorithms.cpp

```
#include
#include
#include

using namespace std;

void swap(std::vector & data, int i, int j)
{
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}

void print(std::vector const & data)
{
std::vector::const_iterator iter = data.begin();

for (; iter != data.end(); ++iter)
{
cout 0)
{
cout & data)
{
int length = data.size();

for (int i = 0; i & data)
{
int length = data.size();

for (int i = 0; i data[j+1])
{
swap(data, j, j+1);
swapped = true;
}
}

if (!swapped) break;
}
}

//useful for small lists and where swapping is expensive
// does at most n swaps
void SelectionSort(std::vector & data)
{
int length = data.size();

for (int i = 0; i & data)
{
int length = data.size();

for (int i = 1; i j; --k)
{
data[k] = data[k-1];
}
data[j] = save;
}
}
}

void Merge(std::vector & data, int lowl, int highl, int lowr, int highr);
void MergeSort(std::vector & data, int low, int high)
{
if (low >= high)
{
return;
}

int mid = low + (high-low)/2;

MergeSort(data, low, mid);

MergeSort(data, mid+1, high);

Merge(data, low, mid, mid+1, high);
}

void Merge(std::vector & data, int lowl, int highl, int lowr, int highr)
{
int tmp_low = lowl;
std::vector tmp;

while (lowl ::const_iterator iter = tmp.begin();

for(; iter != tmp.end(); ++iter)
{
data[tmp_low++] = *iter;
}
}

int Partition(std::vector & data, int low, int high);
void QuickSort(std::vector & data, int low, int high)
{
if (low >= high) return;

int p = Partition(data, low, high);

QuickSort(data, low, p-1);
QuickSort(data, p+1

Solution

Standard functions

There is already a standard swap.

No need o pollute your code with one.

void swap(std::vector & data, int i, int j)

template
void std::swap(T& lhs, T& rhs) // Implements optimal swap for T


OK your shuffle algorithm is ok (better than most people I see try this). But again there is one in the standard

void Shuffle(std::vector & data)

template
void random_shuffle (Iterator first, Iterator last ); // shuffle a container.


Random numbers

Random nmber generation.

NEVER call srand() more than once in a program.

Call it on entry to main and never again.

Don't reinvent the wheel:

int numDigits(int n)


Is this just not implementing

int numDigits(int n) { return int(log10(n) + 1);}


Variable Names

Make your variables names unique and easy to find.

for (int i = 0; i < length; ++i)


Here i is a horrible name.

Imagine the code gets longer over the years and you need to maintain it. Now you need to find all occurences of the variable 'i' to validate that they are being used in the correct array. Think of how many falso positives a search on i is going to give you.

Also it gives no indication as to its use.

I like loop personally (But a lot of people think this is horrible because it does not expresses intention (I disagree with them)). Alternatives would be index, outerLoop etc.

Yes i and 'j` were standard variable names for integer loop variables in fortran and old lecturers who can not get out of the old practice have carried this forward into their teaching of modern languages. In real life where you have good consistent code reviews this would fail a code review and you would be told go go change it (especially on big code bases). Get into the habit of using expressive names.

Indent Style

Your indent style is unique. That is bad. Pick a style that everybody else uses (or one of the big religious groups) but stay consistent (this also helps when tools get involved having a unique style means the some tools may not work as effectively for you).

// Style 1
  if (max < numd)
  {
      max = numd;
  }
// Style 2
  if (max < numd)
      {
      max = numd;
      }
// Style 3
  if (max < numd) {
      max = numd;
  }
// Not a standard style
  if (max < numd)
    {
      max = numd;
    }


And it does not match your outer style:

int numDigits(int n)
{
}

Code Snippets

void swap(std::vector<int> & data, int i, int j)

template<typename T>
void std::swap(T& lhs, T& rhs) // Implements optimal swap for T
void Shuffle(std::vector<int> & data)

template<typename Iterator>
void random_shuffle (Iterator first, Iterator last ); // shuffle a container.
int numDigits(int n)
int numDigits(int n) { return int(log10(n) + 1);}
for (int i = 0; i < length; ++i)

Context

StackExchange Code Review Q#4582, answer score: 10

Revisions (0)

No revisions yet.