Selection Sort Program with Comparison and Swap Counter | Code Help

  • Thread starter heavyc
  • Start date
  • Tags
    Sort
In summary, the conversation is about the speaker's attempt to write a program for a selection sort with a counter for comparisons and swaps. However, they are encountering four error messages and are seeking help. The code provided does not include a main() function, which is necessary in C programs, and there is no call to the predefined functions.
  • #1
heavyc
16
0
Okay here's 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 don't know why so here is the code if anyone can help it would be nice thank you
Code:
 #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;
}
 
Physics news on Phys.org
  • #2
Perhaps sharing the error messages with us might help.
 
  • #3
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.
 

1. What is selection sort and how does it work?

Selection sort is a sorting algorithm that works by repeatedly finding the minimum element from an unsorted list and placing it at the beginning of the list. This process is repeated until the list is fully sorted.

2. How efficient is selection sort compared to other sorting algorithms?

Selection sort has a time complexity of O(n^2), which means that it is less efficient than other sorting algorithms such as quicksort or mergesort. However, it is still more efficient than bubble sort.

3. Can selection sort be used to sort any type of data?

Yes, selection sort can be used to sort any type of data as long as a comparison operator can be defined for the data. This means that it can be used to sort numbers, strings, objects, and other data types.

4. What are the advantages of using selection sort?

Selection sort is a simple and easy-to-implement algorithm that requires minimal memory space. It is also useful for sorting small lists or lists that are almost sorted, as it has a relatively low time complexity for these cases.

5. Are there any downsides to using selection sort?

One major downside of selection sort is that it is not a stable sorting algorithm, which means that the original order of equal elements may not be preserved after sorting. Additionally, it is not very efficient for sorting large lists or lists with a random order of elements.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Replies
63
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top