1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Help with selection sort
  1. C Bubble sort help (Replies: 8)

  2. Merge sort (Replies: 1)

Loading...