For my assignment I need to create a sort_swap function that uses the C++ function swap(); that is already in place. I need to create this so that way it can be used to do other things relating to my program. However, I am not quite sure why my swap function is not working in any of my sorting functions. When printing out the results, I get basically the original list of randomly generated numbers, instead of them being sorted. This program is designed to illustrate and animate the different sorting types, and without the sorting working with my sort_swap functions it is not going to work. I have to keep the sort_swap function.
Any suggestions or recommendations would be appreciated.
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include "rogueutil.h"
//Global swap counter
int swap_ctr;
int compare_ctr;
//namespaces
using namespace std;
using namespace rogueutil;
/******************
Sorting prototypes
******************/
void sort_insertion(vector <int> &v);
void sort_selection(vector <int> &v);
void sort_bubble(vector <int> &v);
void sort_quicksort(vector <int> &v, int L, int R);
/****************************
Partition function prototype
****************************/
int partition(vector <int> &v, int L, int R);
//sort_swap function prototype
void sort_swap(int X, int Y);
//sort_compare function prototype
int sort_compare(int A, int B);
//draw columns function
void col_draw(vector <int> v, int terminal_cols, int terminal_rows);
/***************************************************
MAIN FUNCTION
***************************************************/
int main() {
//set main terminal color to white
setColor(WHITE);
//Find rows and cols of terminal
int terminal_rows = trows();
int terminal_cols = tcols();
//vector v
vector <int> v(terminal_rows - 3);
//randomize seed
srand(time(NULL));
//populate vector with random values 0-20
for(int & i : v)
{
i = rand() % terminal_cols;
}
//copy contents into vector copies
vector <int> v_copy = v;
vector <int> v_copy_insertion = v;
vector <int> v_copy_selection = v;
vector <int> v_copy_bubble = v;
vector <int> v_copy_quick = v;
//Low and high for partition
int L = v_copy_quick[0];
int R = v_copy_quick[terminal_rows - 3];
cls();
swap_ctr = 0;
compare_ctr = 0;
sort_insertion(v_copy_insertion);
for(int x : v_copy_insertion)
cout << x << " ";
cout << endl;
anykey("Press any key to go:");
cls();
swap_ctr = 0;
compare_ctr = 0;
sort_selection(v_copy_selection);
for(int x : v_copy_selection)
cout << x << " ";
cout << endl;
anykey("Press any key to go:");
cls();
swap_ctr = 0;
compare_ctr = 0;
sort_bubble(v_copy_bubble);
for(int x : v_copy_bubble) {
cout << x << " ";
}
cout << endl;
anykey("Press any key to go:");
cls();
swap_ctr = 0;
compare_ctr = 0;
sort_quicksort(v_copy_quick, L, R);
for(int x : v_copy_quick)
cout << x << " ";
cout << endl;
return 0;
}
/*******************************************************************
Sorting functions definitions
*******************************************************************/
//insertion sort
void sort_insertion(vector <int> &v_copy_insertion)
{
for(int i = 1; i < v_copy_insertion.size(); i++)
{
int j = i;
while(j > 0 && v_copy_insertion[j] < v_copy_insertion[j - 1])
{
sort_compare(v_copy_insertion[j], v_copy_insertion[j - 1]);
sort_swap(v_copy_insertion[j], v_copy_insertion[j - 1]);
j--;
}
}
return;
}
//selection sort
void sort_selection(vector <int> &v_copy_selection)
{
for(int i = 0; i < v_copy_selection.size() - 1; i++)
{
int small = i;
for(int j = i + 1; j < v_copy_selection.size(); j++)
{
if(v_copy_selection[j] < v_copy_selection[small])
{
small = j;
}
}
sort_compare(v_copy_selection[i], v_copy_selection[small]);
sort_swap(v_copy_selection[i], v_copy_selection[small]);
}
return;
}
//bubble sort
void sort_bubble(vector <int> &v_copy_bubble)
{
for(int i = 0; i < v_copy_bubble.size() - 1; i++)
{
for(int j = 0; j < v_copy_bubble.size() - 1; j++)
{
if(v_copy_bubble[j] > v_copy_bubble[j + 1])
{
sort_compare(v_copy_bubble[j], v_copy_bubble[j + 1]);
sort_swap(v_copy_bubble[j], v_copy_bubble[j + 1]);
}
}
}
return;
}
//quicksort
void sort_quicksort(vector <int> &v_copy_quick, int L, int R)
{
if(L < R)
{
int p = partition(v_copy_quick, L, R);
sort_quicksort(v_copy_quick, L, p - 1);
sort_quicksort(v_copy_quick, p, R);
}
}
//partition function definition
int partition(vector <int> &v_copy_quick, int L, int R)
{
int result;
int i = L - 1;
int j = R + 1;
int pivot = v_copy_quick[R];
for(;;)
{
while(v_copy_quick[++i] < pivot);
while(v_copy_quick[--j] > pivot);
if(i < j)
{
sort_compare(v_copy_quick[i], v_copy_quick[j]);
sort_swap(v_copy_quick[i], v_copy_quick[j]);
}
else
{
result = i;
break;
}
}
return result;
}
/****************************************************************
Sort swap and sort compare definitions
Steps 7-10
****************************************************************/
//sort_swap function definition
void sort_swap(int X, int Y)
{
setColor(LIGHTRED);
swap(X, Y);
swap_ctr++;
gotoxy(1, 2);
cout << "Swaps: " << swap_ctr;
cout.flush();
cout << endl;
setColor(WHITE);
}
//sort_compare function definition
//ignore still working out logic for this
int sort_compare(int A, int B)
{
setColor(LIGHTGREEN);
compare_ctr = 0;
int greater;
int smaller;
int equalto;
if(A - B < 0)
{
A = smaller;
}
else if(A - B > 0)
{
A = greater;
}
else
{
A = equalto;
}
compare_ctr++;
gotoxy(1, 1);
cout << "Comparisons: " << compare_ctr;
cout.flush();
setColor(WHITE);
return A;
}
//col_draw function definition
//ignore as well
void col_draw(vector <int> &v_copy, int terminal_cols, int terminal_rows)
{
//if problem with color get rid of setChar('#')
//just use cout << '#';
for(int i = 0; i < terminal_cols; i++)
{
cout << '#';
}
}
AS stated previously I tried and tested with the C++ swap function and it worked in sorting the copies of the vector v. I am not understanding why my own function sort_swap would not be working due to the fact that the 2nd line of code is using the swap function. Unless I am missing something entirely, I am lost. Thanks for the help