My code here gets terminated after printing the unsorted array and also gives runtime error on ideone , i am unable to find the error in it . code works fine until the first mergesort in function but gets terminated afterwards without executing merge function . I have tried changing array size but nothing has worked so far . Any help would be appreciated.
#include<stdio.h>
#include<math.h>
void Merge(int arr[],int,int,int,int);
void printArray(int *arr,int n)
{
for (int i = 0; i < n; i++)
{
printf("%d",arr[i]);
printf(" ");
}
printf("\n");
}
void MergeSort(int arr[],int low,int high)
{
int mid;
if(low<high)
{
mid = ceil((low+high)/2);
MergeSort(arr,low,mid-1);
MergeSort(arr,mid,high);
Merge(arr,low,mid-1,mid,high);
}
}
void Merge(int arr[],int low,int mid1,int mid2, int high)
{
int i,c,j;
c = low;
i = low;
j = mid2;
int Temp[high-low+1];
while(i <= mid1 && j<= high)
{
if(arr[i]<arr[j])
{
Temp[c] = arr[i];
i++;
c++;
}
else
{
Temp[c] = arr[j];
j++;
c++;
}
}
while(i<=mid1)
{
Temp[c] = arr[i];
i++;
c++;
}
while(j<=high)
{
Temp[c] = arr[j];
j++;
c++;
}
for(int k=0;k<=high;k++)
{
arr[k] = Temp[k];
}
}
int main(void)
{
int arr[] = {3,5,2,13,12,3,2,13,45};
int n = sizeof(arr)/sizeof(arr[0]);
printf("unsorted array: \n");
printArray(arr,n);
MergeSort(arr,0,n-1);
printf("sorted array: \n");
printArray(arr,n);
return 0;
}
There are several issues:
ceil is not useful as / will perform an integer division and so has already rounded down
Related to this, you should not use mid-1 and mid as arguments in the next recursive calls, but mid and mid+1. The same should be done with the arguments to Merge.
The way you access Temp is wrong. You allocate entries from 0 to high-low, but start your access with a value of c that is low. You should instead start at index 0.
In the very last loop k runs from 0 to high, but that are too many iterations. It should start from low, and then the index access to Temp should again be adapted by using k-low as index.
Here is the corrected code:
void MergeSort(int arr[],int low,int high)
{
int mid;
if(low<high)
{
mid = (low+high)/2; // <--
MergeSort(arr,low,mid); // <--
MergeSort(arr,mid+1,high); // <--
Merge(arr,low,mid,mid+1,high); // <--
}
}
void Merge(int arr[],int low,int mid1,int mid2, int high)
{
int i,c,j;
c = 0; // <--
i = low;
j = mid2;
int Temp[high-low+1];
while(i <= mid1 && j<= high)
{
if(arr[i]<arr[j])
{
Temp[c] = arr[i];
i++;
c++;
}
else
{
Temp[c] = arr[j];
j++;
c++;
}
}
while(i<=mid1)
{
Temp[c] = arr[i];
i++;
c++;
}
while(j<=high)
{
Temp[c] = arr[j];
j++;
c++;
}
for(int k=low;k<=high;k++) // <--
{
arr[k] = Temp[k-low]; // <--
}
}
Important note: I debugged the code for you, but this is something you can do yourself. Inspect the variables as you step through the code with a debugger, and spot where things are unexpected. It takes some time, but it is a skill a coder needs to learn.
Related
Has this function been written correctly?
It seems that something is wrong when I try to run the function with a large number of elements in the array (eg 1000).
Then its appears to stop.
int quick_sort(int n, int tablica[],int b, int a)
{
if(a==n-1 || n==0) return;
if(b==n-1)
{
b=0;
a++;
}
if(tablica[b]>tablica[b+1])
{
bufor=tablica[b];
tablica[b]=tablica[b+1];
tablica[b+1]=bufor;
}
b++;
return quick_sort(n,tablica,b,a);
}
Above code will not work even for a small array, unless the small array is unsorted in a particular way. It compares one element with the next element. If the array is say {4,3,8,7,1} the sort will fail, because it has no mechanism to push 1 to the start of the array.
For larger arrays there are too many recursion and the program hits the stack limit and simply fails.
You can use recursion in quicksort but the number of recursions has to be kept in check. For example for array of size 1000 you don't want to have to more than 1000 recursion. Example:
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
void quicksort(int arr[], int low, int high)
{
if(low < high)
{
int pivot = arr[high];
int i = (low - 1);
for(int j = low; j <= high - 1; j++)
{
if(arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
int pi = i + 1;
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}
int main()
{
int arr[] = { 7,3,6,1,4,8,9,2 };
int arrsize = sizeof(arr) / sizeof(*arr);
quicksort(arr, 0, arrsize - 1);
for(int i = 0; i < arrsize; i++)
printf("%d\n", arr[i]);
return 0;
}
I am doing a merge sort program in C but I'm getting some unexpected output.
Can anyone find out error in the program?
#include<stdio.h>
int array[100],n;
void merge(int l,int m,int h);
void mergesort(int start,int end);
main(){
int i;
printf("Enter Size: ");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&array[i]);
}
mergesort(0, n-1);
for(i=0;i<n;i++){
printf("%d\n",array[i]);
}
}
void mergesort(int start,int end){
int mid;
if(start<end){
mid=(start+end)/2;
mergesort(start,mid);
mergesort(mid+1,end);
merge(start,mid,end);
}
}
void merge(int start,int mid,int end){
int i,j,k;
int a1[100],a2[100];
for(k=0,i=start;i<=mid;i++,k++){
a1[k]=array[i];
}
for(k=0,j=i;j<=end;j++,k++){
a2[k]=array[j];
}
a1[mid]=999;
a2[j]=999;
i=0;j=0;
for(k=start; k <=end; k++)
{
if(a1[i]<=a2[j])
array[k]=a1[i++];
else
array[k]=a2[j++];
}
}
Output:
Enter Size: 5
1 2 3 2 3
2
2
3
3
-1818025592
For larger arrays, the output is more scrambled. I think there is an error in merge() function.
You are terminating your a1 and a2 arrays at the wrong location.
Try changing:
for(k=0,i=start;i<=mid;i++,k++){
a1[k]=array[i];
}
for(k=0,j=i;j<=end;j++,k++){
a2[k]=array[j];
}
a1[mid]=999;
a2[j]=999;
to
for(k=0,i=start;i<=mid;i++,k++){
a1[k]=array[i];
}
a1[k]=999;
for(k=0,j=i;j<=end;j++,k++){
a2[k]=array[j];
}
a2[k]=999;
When you create your temporary arrays:
for (k = 0, i = start; i <= mid; i++, k++) {
a1[k] = array[i];
}
a1[i] = 999;
you put a sentinel value with a high value at the end. But i is the index into the original array. You must use k, the index of the temporary array a1:
for (k = 0, i = start; i <= mid; i++, k++) {
a1[k] = array[i];
}
a1[k] = 999;
Even so, the double control structure with k and i is clumsy. And there's no need to guess a high number; <limits.h> has constants defined for the min and max values of most types:
k = 0;
for (i = start; i <= mid; i++) {
a1[k++] = array[i];
}
a1[k] = INT_MAX;
Here, you increment k when an item is appended. This works even when the assignment happens conditionally, i.e. not in all iterations.
Finally, I would recommend to use exclusive upper bounds. That's the natural way to express ranges in C. mid would then be both the exclusive upper bound of the left array and the inclusive lower bound of the right array.
I have solved it
a[mid + 1]=999
a[k]=999
I want to compare how many swaps and comparisons (<, >, ==, !=) it took for a bubblesort vs. quicksort function to sort an array of unique numbers. The problem is that the quicksort function I use is recursive and I am a bit unsure how to keep track of swaps comparisons. Tried to use pointers to keep track of the count but was unsuccessful. Could anyone help me?
my bubblesort:
void bubble_sort(int arr[], int max)
{
int i=0, j, temp, flag;
int swap=0, comp=0;
while(1)
{
flag = 0;
for (j = 0 && comp++; j < max - i - 1; j++)
{
comp++;
if (arr[j] > arr[j+1])
{
swap++;
/* Swapping */
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag=1;
}
}
i++;
comp++;
if (flag == 0)
{
printf("number of swaps: %d\n",swap);
printf("number of comparisons: %d \n\n",comp);
break;
}
}
}
my quicksort:
void quicksort(int arr[],int first,int last)
{
int pivot,j,temp,i;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(arr[i]<=arr[pivot]&&i<last)
i++;
while(arr[j]>arr[pivot])
j--;
if(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
temp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=temp;
quicksort(arr,first,j-1);
quicksort(arr,j+1,last);
}
}
solution:
void quick_sort(int arr[],int first,int last, int *swap, int *comp)
{
int pivot,j,temp,i;
if(++(*comp) && first<last)
{
pivot=first;
i=first;
j=last;
while(++(*comp) && i<j)
{
while(++(*comp) && arr[i]<=arr[pivot]&&i<last)
i++;
while(++(*comp) && arr[j]>arr[pivot])
j--;
if(++(*comp) && i<j)
{
++(*swap);
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
++(*swap);
temp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=temp;
quick_sort(arr,first,j-1, swap, comp);
quick_sort(arr,j+1,last, swap, comp);
}
}
Either use a global variable as suggested in the comments :
int _SwapCount = 0;
void quicksort(int arr[],int first,int last)
{
...
//whenever you swap
_SwapCount++;
or take a pointer to an int as a parameter:
void quicksort(int arr[],int first,int last, int* swapCount)
{
...
//whenever you swap
(*swapCount)++;
//recursion
quicksort(arr,first,j-1, swapCount);
...
and output the swapCount once the top level quicksort has completed.
Edit : initially mis-read the tag as c#;
I'm having trouble tracking down a "conditional jump or move depends on uninitialised value(s)" valgrind error in some mergesort code I wrote. The code sorts things properly, but I know there's a memory leak from a variable not being initialized. Valgrind tells me it's in the m_sort portion of my code, but I'm not sure what's not being initialized.
void merge(int *C, int *A, int n, int *B, int m) {
int i, j, k;
for (i=0,j=0,k=0; k<(n+m); k++) {
if (i==n) {
C[k] = B[j];
j++;
}
else if (j==m) {
C[k] = A[i];
i++;
}
else {
if (A[i] < B[j]) {
C[k] = A[i];
i++;
}
else {
C[k] = B[j];
j++;
}
}
}
}
void m_sort(int *A, int *B, int left, int right) {
int mid;
if (right >left) {
mid =((right + left)/2);
m_sort(B, A, left, mid);
m_sort(B, A, mid+1, right);
merge(A+left, B+left, mid-left+1, B+mid+1, right-mid);
}
}
void merge_sort(int *a, int left, int right)
{
int * T = NULL;
int i;
T = (int*)malloc((right+1)*sizeof(int));
for (i=0; i<right; i++)
{
T[i] = a[i];
}
m_sort(a,T,left,right);
free(T);
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_SIZE 100
I am trying to quicksort an array of 2D points based upon their distance from the origin but my code hits a Seg fault after the first scanf.
typedef struct point{
double x;
double y;
double dist;
} point;
void sortpoints(point arr[], int low, int high);
void printpoints(point arr[], int n);
Sortpoints is just a manipulation of a quicksort in ordering an array of Point structs depending on the value of Point.dist
void sortpoints(point arr[], int low, int high){
int piv, i, j;
piv = low;
i = low;
j = high;
point box;
if(low < high){
while(i<j){
while((arr[i].dist)<=(arr[piv].dist) && i<= high)
i++;
}
while((arr[j].dist) > (arr[piv].dist) && j>= low)
j--;
}
if(i<j){
box = arr[i];
arr[i] = arr[j];
arr[j] = box;
}
box = arr[j];
arr[j] = arr[piv];
arr[piv] = box;
sortpoints(arr, low, j-1);
sortpoints(arr, j+1, high);
}
Printpoints just prints the points in order of their distances from the origin
void printpoints(point arr[], int n){
int i; for(i = 0; i <= n; i++){
printf("(%.2lf, %.2lf)\n", arr[i].x, arr[i].y);
}
}
The user enters the number of points and value of the points in the form (point.x, point.y)
int main(){
point pointa;
point pointarray[MAX_SIZE];
int n=0;
printf("how many points would you like to enter?\n");
scanf("%d", &n);
if(n<MAX_SIZE){
int i;
for(i=0; i<n ; i++){
scanf("(%lf,%lf)", &pointa.x, &pointa.y);
pointa.dist = sqrt(pointa.x*pointa.x + pointa.y*pointa.y);
pointarray[i] = pointa;
}
sortpoints(pointarray, 0, n-1);
printpoints(pointarray, n);}
else{
printf("sorry, not a valid array size\n");
}
return 0;
}
I've got stack overflow.
Your program calls function sortpoints, and calls itself too (sortpoints calls sortpoints). But i cant see where sortpoints must stop!
As a result, j-1 goes under null, and arr[j] is not valid at all.
When I changed this line:
scanf("(%lf,%lf)", &pointa.x, &pointa.y);
To this:
scanf("%lf,%lf", &pointa.x, &pointa.y);
The point values were actually being read in, with the parens there nothing gets read in, I don't see anything in the documentation on scanf about this though, is anyone familiar with this?
You sorting code seems to go into infinite recursion and cause a stack overflow however.
Your issue is with your braces. I have reformatted your sortpoints function slightly so it is clearer where each basic block actually is.
void sortpoints(point arr[], int low, int high){
int piv, i, j;
piv = low;
i = low;
j = high;
point box;
if(low < high){
while(i<j){
while((arr[i].dist)<=(arr[piv].dist) && i<= high)
i++;
}
while((arr[j].dist) > (arr[piv].dist) && j>= low)
j--;
}
if(i<j){
box = arr[i];
arr[i] = arr[j];
arr[j] = box;
}
box = arr[j];
arr[j] = arr[piv];
arr[piv] = box;
sortpoints(arr, low, j-1);
sortpoints(arr, j+1, high);
}
It should be evident now that your segfault is the result of accidental infinite recursion. This is a good argument for using { } for your loops, even with a single statement.