Troubleshooting a Segmentation Fault in a Selection Sort

AI Thread Summary
The discussion centers around issues with a C code implementation for sorting algorithms, specifically a selection sort and a request for a merge sort example. The user experiences a segmentation fault when running their selection sort code. Key points include the identification of an error in the `scanf` statement, where the correct usage requires the address operator `&` to avoid dereferencing issues. Additionally, the loop conditions are incorrect; the outer loop should run while `i < 9` and the inner loop while `j < 10`. It is also noted that the provided sorting algorithm resembles a bubble sort rather than a selection sort. The user seeks assistance in correcting these issues and implementing a merge sort.
george2625
Messages
3
Reaction score
0
please help me with a c code for the merge sort... and i am getting a segmentation fault in running this selection sort,,, please help...

#include <stdio.h>

int main( )
{
int arr[10];
int i,j,temp;
for ( i = 0 ; i<10 ; i++ )
{
printf("enter the numbers: \n");
scanf("%d \n", arr);
}
for ( i = 0 ; i <8 ; i++ )
{
for ( j = i + 1 ; j <9 ; j++ )
{
if ( arr > arr[j] )
{
temp = arr ;
arr = arr[j] ;
arr[j] = temp ;
}
}
}
for ( i = 0 ; i <10 ; i++ )
printf("the sorted array is: %d \n", &arr);
return 0;
}
 
Technology news on Phys.org


george2625 said:
please help me with a c code for the merge sort... and i am getting a segmentation fault in running this selection sort,,, please help...

scanf("%d \n", arr);

The problem is in your scanf statement. Scanf needs a reference and "arr" is already de-referenced. You need to prefix it with "&".
 
You probably wanted those loops to be ... ; i < 9; ... and ... ; j < 10; .... Also this algorithm is not a merge sort, it's a bubble (down) sort.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top