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;
}
}
}
selection sortsort numbers
Page 1 of 1
1 Replies - 643 Views - Last Post: 29 April 2009 - 03:20 AM
Replies To: selection sort
#2
Re: selection sort
Posted 29 April 2009 - 03:20 AM
Hi there
Please
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.
you also need to provide a size declarator when declaring an array.
Please
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
|
|

New Topic/Question
Reply




MultiQuote



|