Troubleshooting a Segmentation Fault in a Selection Sort

The merge sort algorithm is very different and more complex, and it's not easy to explain in a few lines. In summary, the code provided has two main issues: the scanf statement needs a reference and the for loops are using incorrect indices for a bubble sort algorithm. Also, the provided code is not for a merge sort, it is for a bubble sort.
  • #1
george2625
3
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
  • #2


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 "&".
 
  • #3
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.
 

1. What is a segmentation fault?

A segmentation fault, also known as a segfault, is an error that occurs when a program attempts to access a memory location that it is not allowed to access. This usually happens when a program tries to access memory outside of its allocated space, resulting in a program crash.

2. Why does a segmentation fault occur during a selection sort?

A segmentation fault can occur during a selection sort if there is an error in the code that causes the program to try to access memory that it is not allowed to access. This can happen if the array being sorted is not properly initialized or if there is an error in the loop conditions or index values.

3. How can I troubleshoot a segmentation fault in a selection sort?

To troubleshoot a segmentation fault in a selection sort, you can use debugging tools such as a debugger or a memory profiler. These tools can help you identify the exact location and cause of the segmentation fault, allowing you to fix the error in your code.

4. Are there any common mistakes that can lead to a segmentation fault in a selection sort?

Yes, there are several common mistakes that can lead to a segmentation fault in a selection sort. These include using incorrect loop conditions or index values, not properly initializing the array, and not handling edge cases such as empty arrays or arrays with only one element.

5. How can I prevent a segmentation fault in a selection sort?

To prevent a segmentation fault in a selection sort, it is important to thoroughly test your code and handle all possible edge cases. It is also helpful to use debugging tools and techniques to catch any errors before they result in a segmentation fault. Writing clean and well-structured code can also help prevent errors and crashes in your program.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
759
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top