C++ selection sort issue

Hello..
I am currently having an issue with the SelectionSort function as it is not properly sorting the index.. Any help is appreciated!


#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

struct Personality_t
{
double neuroticism;
double extroversion;
};

struct Physique_t
{
int weight;
int wingspan;
};

struct Penguin_t
{
int ID;
string name;
Personality_t personality;
Physique_t physique;
};

void wingspanSort(struct Penguin_t *data, int size, int wingspan[50])
{
int min_index, min_value, temp;
int i, j;

for (i=0; i < size-1; i++)
{
//set the position to the current index of the array
min_index = i;

//One by one move boundary of unsorted subarray
for (j=i+1; j < size; j++)
{
//Find the minimum element in unsorted array
if (data[j].physique.wingspan < data[min_index].physique.wingspan)
{
min_index=j;
min_value=data[j].physique.wingspan;
wingspan[i] = data[min_index].ID;
cout << "i: "<<i<< " min_index: "<< min_index << " min_value: "<< min_value<< " ID: "<< wingspan[i]<<endl;
}
}

// if position is no longer = i then a smaller value must have been found; so swap it
if (min_index != i)
{
temp = data[i].physique.wingspan;
data[i].physique.wingspan = data[min_index].physique.wingspan;
data[min_index].physique.wingspan = temp;
}
}
}

void neurosisCategory(struct Penguin_t *data, int size, int neurosis[50][4])
{
int i, j;

// initialize array
for (i=0; i<size; i++)
{
for (j=0; j<4; j++)
{
neurosis[i][j] = -1;
}
}

for (i=0; i<size; i++)
{
if ( data[i].personality.neuroticism >=1 && data[i].personality.neuroticism < 4)
neurosis[i][0] = i;
if ( data[i].personality.neuroticism >=4 && data[i].personality.neuroticism < 6)
neurosis[i][1] = i;
if ( data[i].personality.neuroticism >=6 && data[i].personality.neuroticism < 8)
neurosis[i][2] = i;
if ( data[i].personality.neuroticism >=8 && data[i].personality.neuroticism <= 10)
neurosis[i][3] = i;
cout << data[i].personality.neuroticism << "|"<< neurosis[i][0] << "|" << neurosis[i][1] << "|" << neurosis[i][2] << "|" << neurosis[i][3] << endl;
}
}

void selectionSort(int arr[], int size)
{
int min_index, temp;

for (int i=0; i< size-1; i++)
{
//set the position to the current index of the array
min_index = i;

//One by one move boundary of unsorted subarray
for (int j=i+1; j < size; j++)
{
//Find the minimum element in unsorted array
if (arr[j] < arr[min_index])
min_index=j;
}
// if position is no longer = i then a smaller value must have bee found; so swap it
if (min_index != i)
{
temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
}

void printArray(int arr[], int size) {
for (int i=0; i<size; i++)
{
cout << arr[i] << endl;
}
}

void printStruct(struct Penguin_t *data, int size) {
for (int i=0; i<size; i++)
{
cout << "ID:"<< data[i].ID<< " wingspan: "<< data[i].physique.wingspan << endl;
}
}

int main(int argc, const char * argv[])
{
const int array_size = 50;
const string name_array[array_size] = {
"Abe","Amy","Bob","Ben","Cam","Cal","Dan","Don","Eve","Eli",
"Eva","Fay","Gil","Gus","Guy","Hal","Ian","Ike","Ira","Jan",
"Jay","Jeb","Jed","Joe","Kim","Kay","Lee","Liz","Lou","Leo",
"Max","Meg","Moe","Nat","Pat","Rae","Ray","Rob","Rod","Ron",
"Roy","Sal","Sue","Tim","Tom","Tre","Val","Von","Vic","Zoe"
};

//declare penguin data array
Penguin_t *dataset;
dataset = new Penguin_t [array_size];

// initialize the dataset
for (int i=0; i<50; i++)
{
dataset[i].ID = i;
dataset[i].name = name_array[i];
dataset[i].physique.weight = rand() % 17 + 4;
dataset[i].physique.wingspan = rand() % 21 + 4;
dataset[i].personality.neuroticism = rand() % 900 / double(100) + 1;
dataset[i].personality.extroversion = (rand() % 1000 / double(100)) - 5;

}

// define, sort and load and wingspan array
int wingspan_arr[50]; //define wingspan array
//printStruct(dataset, array_size);
wingspanSort(dataset, array_size, wingspan_arr);
//cout<<endl<<endl;
//printStruct(dataset, array_size);
//cout<<endl<<endl;
//printArray(wingspan, array_size);

// define and load neurosis array
int neurosis_arr[array_size][4];
neurosisCategory(dataset, array_size, neurosis_arr);

// test for top contenders

// identify super penguin




return 0;
}
Last edited on
Divide and conquer.

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
#include <iostream>

std::size_t position_of_smallest_element( const int a[], std::size_t num_elements )
{
    std::size_t pos = 0 ;
    for( std::size_t i = 0 ; i < num_elements ; ++i )
        if( a[pos] > a[i] ) pos = i ;
    return pos ;
}

void swap_elements_at( int a[], std::size_t pos_1, std::size_t pos_2 )
{
    if( pos_1 != pos_2 )
    {
        int temp = a[pos_1] ;
        a[pos_1] = a[pos_2] ;
        a[pos_2] = temp ;
    }
}

void selection_sort( int a[], std::size_t num_elements )
{
    if( num_elements > 1 )
    {
        // bring the smallest element to the front (to position zero)
        swap_elements_at( a, 0, position_of_smallest_element( a, num_elements ) ) ;

        // sort the remaining elements (from position one onwards) in the array
        selection_sort( a+1, num_elements-1 ) ;
    }
}

int main()
{
    int arr[] { 56, 12, 92, 77, 93, 29, 45 } ;
    selection_sort( arr, sizeof(arr) / sizeof( arr[0] ) ) ;

    for( int v : arr ) std::cout << v << ' ' ;
    std::cout << '\n' ;
}

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