Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

The program compiles but the numbers do not sort and it ends with "segmentation fault". Help? I am using a c++ compiler

void Selection_sort(int [], int); 
void Swap (int*, int &, int &);
void Display_list(int [], int);

int main (){
const int   count=10; // number of input values
int     i=0;

int List[count];

cout << "Enter " << count << " values: ";

for (i=0; i<count; i++)
{
cin >> List[i];
}

Display_list(List, count); // before sorting

Selection_sort(List, count);



Display_list(List, count); // after sorting

return 0;
}

void Selection_sort(int L[], int cnt){
int largest;
int i;
int n=i-1;

for(i=1; i<10; i++)
{
if (L[i]>largest)
largest= L[i];
Swap(L,largest,n);
}
}

void Swap (int l[],int &A, int &B){
int temp;
temp=l[B];
l[B]=l[A];
l[A]=temp;


}

void Display_list(int L[], int cnt){
cout<<"The numbers in this array are : \n"<<endl;
for (int i=0; i<cnt; i++)
cout<<L[i]<<endl;   
cout<<"\n"<<endl; 
}
share|improve this question
Can you please edit your post to show the entire program, not just those two functions? Also, what compiler are you using? What flags are you passing it? – Andy Lester Nov 28 '12 at 6:32
1  
1. Looks like you only implemented the inner loop of selection sort. 2. i and largest are used uninitialized. 3. cnt is unused. 4. in Swap, there's no reason for A and B to be references. 5. You should use consistent indentation. 6. You should share the code of the caller, as it might explain the crash (though the reason is probably that n depends on uninitialized use of i). 7. This is off-topic for code review. – asveikau Nov 28 '12 at 6:42
Try posting this question on stackoverflow.com. – Loki Astari Nov 28 '12 at 19:11
PS. Fix your indention. People are unlikely to help if they think you don't care. Also make sure it compiles (you are missing an include). – Loki Astari Nov 28 '12 at 19:13

closed as off topic by Corbin, asveikau, Brian Reichle, codesparkle, Trevor Pilley Nov 28 '12 at 15:30

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer


void Selection_sort(int L[], int cnt){
int largest;    
int i;

largest and i should be initialized because you use i directly:

int n=i-1;

or maybe you meant cnt:

   #include <climits>

   int largest = std::numeric_limits<int>::min();
   int n = cnt - 1;

Also add some asserts inside your functions to check the indices just to be on the safe side.

I would also recommend using a std::vector instead of a native array, it offers much more flexibility and is safer to use

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.