Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Help with selection sort

  1. Nov 20, 2005 #1
    Okay heres the deal I am writing a program so that i could put a counter into a selection sort and count the comparsions and the swaps but when i wrote thw code I get 4 error messages and I dont know why so here is the code if anyone can help it would be nice thank you
    Code (Text):
     #include <iostream.h>
    #include <stdlib.h>
    #include <cstddef>
    typedef int Select;
    void selectionsort(Select theArray[], int n)
    {
    n = 5;
    for (int last = n-1; last >= 1; --last)
    {
    int largest = indexoflargest(theArray, last + 1);
    swap(theArray[largest], theArray[last]);
    }
    }
    int indexoflargest(const Select theArray[], int size)
    {
    int indexsofar = 0;
    for (int currentindex = 1; currentindex < size; ++currentindex)
    {
    if(theArray[currentindex] > theArray[indexsofar])
    indexsofar = currentindex;
    }
    return indexsofar;
    }
    void swap(Select& x, Select& y)
    {
    Select temp = x;
    x = y;
    y = temp;
    }
     
     
  2. jcsd
  3. Nov 20, 2005 #2

    DaveC426913

    User Avatar
    Gold Member

    Perhaps sharing the error messages with us might help.
     
  4. Nov 21, 2005 #3

    mezarashi

    User Avatar
    Homework Helper

    The error messages would help. And also, if that is your program in its whole, you don't have a main() function which every C program must have. There is nothing calling your predefined functions.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook