Error in QUICKSORT . Unable to fix the error - c

I was working on QUICKSORT and I wrote this whole program by myself. The code is as follows
#include<stdio.h>
#include<stdlib.h>
void quicksort(int*,int,int);
int partition(int*,int,int);
swap(int*,int*);
int main()
{
int *arr,n,i;
printf("\nEnter the size of the array");
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
printf("\nEnter the elements of the array one by one");
for(i=0;i<n;i++)
{
scanf("%d",arr+i);
}
quicksort(arr,0,n-1);
printf("\nThe Sorted Array is as follows");
for(i=0;i<n;i++)
{
printf("%d",*(arr+i));
}
return 0;
}
void quicksort(int* arr,int a,int b)
{
int c;
if(a<b)
c=partition(arr,a,b);
quicksort(arr,a,c-1);
quicksort(arr,c+1,b);
}
int partition(int* arr,int a,int b)
{
int x,y,index=*(arr+b);
x=a,y=b-1;
while(x<y)
{
if(*(arr+x)<index)
{
x++;
}
if(index<*(arr+y))
{
y--;
}
swap((arr+x),(arr+y));
}
swap((arr+x),(arr+b));
return x;
}
swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
I tried to run this program but after the array input it says segmentation error. I spent few minutes looking at the code and checked the code again and again but I do not seem to move anywhere.
can someone tell me where is the mistake.

What should be happening if a is greater than or equal to b? this should solve the segmentation problem.
void quicksort(int* arr, int a, int b) {
int c;
if (a < b) {
c = partition(arr,a,b);
quicksort(arr, a, c-1);
quicksort(arr, c+1, b);
}
}

Related

Quicksort: Why wrong output

I want to sort the given array using quicksort.
So I wrote this code in c for quicksort algorithm but its not giving me correct answer.Its giving me the same output as the input.
eg: If i give input as
3
2
1
then it is giving
Output:
3
2
1
Please help and tell me where is the mistake.
#include<stdio.h>
void quicksort(int a[],int p,int r);
int partition(int a[],int p,int r);
void swap(int a,int b);
int main()
{
int n,i,p,r;
printf("ENter no of elements");
scanf("%d",&n);
int a[n];
printf("ENter the elements");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
p=1;
r=n;
quicksort(a,1,n);
printf("\n");
for(i=1;i<=n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}
void quicksort(int a[],int p,int r)
{
int q;
if(p<r)
{
q = partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}
int partition(int a[],int p,int r)
{
int x,i,j;
x=a[r];
i=p-1;
for(j=p;j<=r-1;j++)
{
if(a[j]<=x)
{
i=i+1;
swap(a[i],a[j]);
}
}
swap(a[i+1],a[r]);
return (i+1);
}
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
}
void swap(int a,int b)
This line is taking the two parameters as copies of the values. The swapping is done on he copies.
void swap(int* a, int* b)
This is the C-style of passing parameters by reference. You need to pass the references then:
swap(&a[i], &a[j]);

Error in compilation while trying to write a program on bubble sort using pointers

I tried my hand in writing a program in C to "bubble sort" a sequence of numbers, obtained as input, using pointers. It was as follows:
#include<stdio.h>
void swap(int *p,int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
void sort(int *a[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
swap(a[j],a[j+1]);
}
}
}
int main()
{
int p[40],b,i;
printf("Enter the number of elements in the sequence: \n");
scanf("%d",&b);
printf("Enter the elements of the sequence: \n");
for(i=0;i<b;i++)
{
scanf("%d",p[i]);
}
sort(p,b);
printf("The sorted sequence is: \n");
for(i=0;i<b;i++)
{
printf("%d \n",p[i]);
}
return 0;
}
However, the program did not compile. It showed the following error message:
The error message shows:
error 139 - Argument no 1 of 'sort' must be of type '<ptr><ptr>int', not 'int[40]'
Can anybody tell me how I should correct my program so that it gets compiled and gives a correct output?
ADDENDUM: The following is the corrected code, as asked for-
#include<stdio.h>
void myswap(int *p,int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
void sort(int a[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
myswap(&a[j],&a[j+1]);
}
}
}
int main()
{
int p[40],b,i;
printf("Enter the number of elements in the sequence: \n");
scanf("%d",&b);
printf("Enter the elements of the sequence: \n");
for(i=0;i<b;i++)
{
scanf("%d",&p[i]);
}
sort(p,b);
printf("The sorted sequence is: \n");
for(i=0;i<b;i++)
{
printf("%d \n",p[i]);
}
return 0;
}
From a short look two mistakes found:
void sort(int *a[],int n)
should be
void sort(int a[],int n)
and
swap(a[j],a[j+1])
should be
swap(&a[j],&a[j+1])
a[j] is just an integer you need to take the address of the element placing & since swap declaration requires pointers.

C program for quicksort(recursive)

The following c program is a quicksort recursive program. Although I have written this code according to the Cormen explanation, yet it is incorrectly sorting the input.For example it is sorting the input 3,8,1 to 3,1,8. Thanks a lot in advance for finding the mistake
#include<stdio.h>
void printa(int a[],int size)
{
int i;
printf("\n");
for(i=0;i<size;i++)
{
printf("%d\n",a[i]);
}
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int partition(int a[],int p,int r)
{
int i,j,x;
i=p-1;
x=a[r];
for(j=p;j<r;j++)
{
if(a[j]<=x)
{
i=i+1;
swap(&a[i],&a[j]);
}
swap(&a[i+1],&a[r]);
}
return i+1;
}
void quicksort(int a[],int p,int r)
{
if(p<r)
{
int q;
q=partition(a,p,r);
quicksort(a,p,q);
quicksort(a,q+1,r);
}
}
main()
{
int a[50],i,size;
printf("enter the size of the array\n");
scanf("%d",&size);
printf("enter the elements of the array\n");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
quicksort(a,0,size-1);
printa(a,size);
}

Merge two arrays without sorting using array

I am trying to merge to arrays without sorting (add one then another) using pointer method but its just printing the first array and then garbage values. What i am trying to do is just combine 2 arrays in one big array. No sorting required(at least for now).
void getarray(int*,int);
int merge(int*,int*,int,int,int*);
main()
{
int a[10],b[10],c[20];
int i,j,n,m,size;
clrscr();
printf("Enter no. of elements in FIRST array: ");
scanf("%d",&n);
getarray(a,n);
printf("Enter no. of elements in SECOND array: ");
scanf("%d",&m);
getarray(b,m);
merge(a,b,n,m,c);
printf("\nMerged Array: \n");
for(i=0;i<n+m;i++)
{
printf("\t%d\t",c[i]);
}
getch();
return 0;
}
void getarray(int *x, int y)
{
int i;
for(i=0;i<y;i++)
{
scanf("%d",x+i);
}
}
int merge(int *a, int *b,int n,int m,int *c)
{
int i,j;
for(i=0;i<n;i++)
{
*(c+i) = *(a+i);
}
for(j=i;j<i+m;j++)
{
*(c+j) = *(b+j);
}
}
Alternatively you can use (assuming c is large enough):
void merge(int *a, int *b,int n,int m,int *c) {
memcpy(c, a, sizeof(int)*n);
memcpy(c+n, b, sizeof(int)*m);
}
You would need to include string.h.
int merge(int *a, int *b,int n,int m,int *c)
{
int i,j;
for(i=0;i<n;i++)
{
*(c+i) = *(a+i);
}
for(j=0;j<m;j++)
{
*(c+n+j) = *(b+j);
}
}

Selection sort on array in C

I'm trying to create a simple(?) selection sort program in C that selects the largest integer of an integer array and places it in the location a[n-1], places the second largest number in a[n-2], etc until the smallest number is placed in a[0]. I've run through the below code on paper and it seems like it should work, but when I compile it I'm getting faulty results. Am I missing something obvious?
/* The program implements selection sort*/
#include <stdio.h>
#include "simpio.h"
#define n 5
void GetArray(int a[]);
void SelectionSort(int a[]);
int FindMax(int a[], int high);
void swap(int a[], int p1, int p2);
void PrintArray(int a[]);
main()
{
int a[n];
GetArray(a);
SelectionSort(a);
PrintArray(a);
getchar();
}
void GetArray(int a[])
{
int i;
for(i=0;i<n;i++)
{
printf("Enter integer# %d", i+1);
a[i]=GetInteger();
}
}
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,i);
swap(a,max,(n-1-i));
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=high;i<n;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
void swap(int a[], int p1, int p2)
{
int temp;
temp=a[p2];
a[p2]=a[p1];
a[p1]=temp;
}
void PrintArray(int a[])
{
int i;
for(i=0;i<n;i++)
printf("a[%d]=%d\n", i, a[i]);
}
Change these method to:
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,n-i-1);
swap(a,max,n-i-1);
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=0;i<high;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
I actually tested my answer and it works.
Selection sort is process of comparing minimum element from the list and placing from the least index.
Now consider below code snippet.
public void selectionSort(int[] elements) {
for(int i=0;i<elements.length;i++) {
int minPosition = i;
for(int j=i+1;j<elements.length;j++) {
if(elements[minPosition]>elements[j])
minPosition = j;
}
int temp = elements[i];
elements[i] = elements[minPosition];
elements[minPosition] = temp;
}
}
Thanks for reading, let me know feedback to improve from myside
Shouldn't:
max=FindMax(a,i);
swap(a,max,(n-1-i));
Be:
max=FindMax(a,i);
swap(a,max,i);
otherwise, next time through the loop, you'll find the same max value in the top position in the array.
A very basic implementation of selection sort
#include<stdio.h>
main()
{
int i,j,n=7,a[]={1,2,5,3,8,9,5},key;
for(j=1;j<n;j++)
{
key=a[j]; //a[j] is the new element to be added to the sorted
//sequence
i=j-1;
while(i>=0 && key<a[i]) //traverse through the sorted sequence
{a[i+1]=a[i];i--;} //until the place of key is found
a[i+1]=key;
}
for (j=0;j<n;j++)
printf("%d",a[j]);
}
#include<stdio.h>
#include<conio.h>
int removex(int arr[],int small,int n)
{
int i=0;
for(;i<n;i++)
if(arr[i]==small) //searching that no to delete
break;
for(;i<n-1;i++)
arr[i]=arr[i+1]; //delete by overloading no
return n-1;
}
void selectSort(int arr[],int sort[],int n)
{
int j=0,k=0,small;
while(n!=0)
{
small=arr[0];
for(j=0;j<n;j++)
if(arr[j]<small)
small=arr[j]; //finding smallest no
sort[k++]=small;
n=removex(arr,small,n); //removing that from list as we included that no into sorted list
}
}
void main()
{
int arr[10],arr2[10],i,n;
clrscr();
printf("Enter how many elements");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
selectSort(arr,arr2,n);
printf("sorted list is\n");
for(i=0;i<n;i++)
printf("%d\n",arr2[i]);
getch();
}

Resources