# selection sort

Page 1 of 1

## 1 Replies - 1005 Views - Last Post: 29 April 2009 - 03:20 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=102302&amp;s=920b3d977fcff0f40e8c7a9a29c4be2a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 krazykid636

Reputation: 0
• 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

• フカユ

Reputation: 1032
• Posts: 4,289
• Joined: 14-February 08

## Re: selection sort

Posted 29 April 2009 - 03:20 AM

Hi there

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.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }