selection sort

sort numbers

Page 1 of 1

1 Replies - 1208 Views - Last Post: 29 April 2009 - 03:20 AM Rate Topic: -----

#1 krazykid636  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-April 09

selection sort

Posted 29 April 2009 - 01:51 AM

my teacher gave me an assignment called selection sort algorithm and i have to do the following:
1. get a list of unsorted numbers
2. set a marker for the unsorted section at the front of the list
3. repeat 4-6 until one number remains in the unsorted section
4. compare all unsorted numbers in order to select the smallest
5. swap this number with the first number in the unsorted section
6. advance the marker to the right one possition
7. stop

and this is what i manage to get but when i ran the program it kept on tell me "fatal error" so i'm lost when it tells me that or tell me another way of doing this. can some one help me out please.

#include <iostream>
using namespace std;

int main()

void selectionSort;
int arr[], int n;


int i, j, minIndex, tmp;

for (i = 0; i < n - 1; i++)

minIndex = i;

for (j = i + 1; j < n; j++)

if (arr[j] < arr[minIndex])

minIndex = j;

if (minIndex != i)

tmp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = tmp;




Is This A Good Question/Topic? 0
  • +

Replies To: selection sort

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: selection sort

Posted 29 April 2009 - 03:20 AM

Hi there

Please :code:

There are quite a few errors in your code there, you missed the open curly bracket after int main() and the end curly bracket, when writing if statements or for loops it is a lot easier to use the curly brackets than not as it can become decidedly difficult to see where certain statements end.

Also you have declared a string as void. If this is to be a function you need the () parenthesis.
// this
void selectionSort;

//should be this
void selectionSort();

you also need to provide a size declarator when declaring an array.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1