quicksort program provides partially sorted output - c

Below simple quick sort code with last element as pivot,almost works but except the last element fails to get sorted. Any thoughts where this program went wrong?
Here's the output:
$a.out
4 3 5 2 1 3 2 3 //input
1 2 2 3 3 3 5 4 //output
Simple swap looks fine
void swap ( int* a, int* b )
{
int t = *a;
*a = *b;
*b = t;
}
Hmm..fine too ..may issue with end?
int partition(int a[],int start,int end){
int pivot = a[end];
int pindex=start;int i;
for ( i=start; i <= end-1; i++){
if (a[i] <= pivot){
swap(&a[i],&a[pindex]);pindex++;
}
}
swap(&a[pindex],&a[pivot]);
return (pindex + 1);
}
Surely looks good.
void quicksort(int a[],int start,int end){
int pindex;
if (start < end){
pindex = partition(a,start,end-1);
quicksort(a,start,pindex-1);
quicksort(a,pindex+1,end);
}
}
Simple main call
int main(){
int a[8] = {4, 3, 5, 2, 1, 3, 2, 3};
int i=0;
for(i=0;i<8;i++)
printf(" %d", a[i]);
quicksort(a,0,8);
printf("\n");
for(i=0;i<8;i++)
printf(" %d", a[i]);
}

Okay couple of changes
As doptimusprime pointed return pindex
int partition(int a[],int start,int end){
int pivot = a[end];
int pindex=start;int i;
for ( i=start; i <= end-1; i++){
if (a[i] <= pivot){
swap(&a[i],&a[pindex]);pindex++;
}
}
swap(&a[pindex],&a[end]);
return (pindex);
}
Adjust your quicksort function accordingly
void quicksort(int a[],int start,int end){
int pindex;
if (start < end){
pindex = partition(a,start,end-1);
quicksort(a,start,pindex); // no pindex-1
quicksort(a,pindex+1,end);
}
}

Please review partition function.
It should return
return pindex;
instead of pindex+1.
Because, just take the following case:
1 2 3 4 5
As 5 is chosen as pivot, it should return 4, not 5 as pivot index.
Check for the invariant that pindex must lie between start and end (both inclusive). If pivot is to be lie at end, it must not pass the end.
Generally, partition starts from both end. You are starting it from the one end. Try to do it both the end for efficiency. Otherwise, in the 1 2 3 4 5, you will keep swapping with the same element (1 with 1, 2 with 2 and so on).
And in the partition:
swap(&a[pindex],&a[pivot]);
should be
swap(&a[pindex],&a[end]);
pivot was a value, not an index.
Another change, you required quicksort,
if (start < end){
pindex = partition(a,start,end-1);
//As here index is one past last, so make it start..pindex
quicksort(a,start,pindex);
quicksort(a,pindex+1,end);
}
Your partition function should be
int partition(int a[],int start,int end){
int pivot = a[end];
int pindex=start;int i;
for ( i=start; i <= end-1; i++){
if (a[i] <= pivot){
swap(&a[i],&a[pindex]);pindex++;
}
}
swap(&a[pindex],&a[end]);
return (pindex);
}
See https://en.wikipedia.org/wiki/Quicksort#Lomuto_partition_scheme. This partition scheme is used here.

Your program fails if someone type negative numbers.
Why are you complicating things like that ?
Here is a simple sort:
#include <stdio.h>
void bubble_sort(int *array, int length){
int i,j, k, temp;
for (i = 0 ; i < length-1; i++){
for (k = 0 ; k < length-i-1; k++){
if (array[k] > array[k+1]){
temp = array[k];
array[k] = array[k+1];
array[k+1] = temp;
}
}
}
printf("The sorted Array List:\n\n");
for ( j = 0 ; j < length ; j++ ){
printf("%d ", array[j]);
}
}
int main(void){
int array[8] = {4, 3, 5, 2, 1, -1, 2, 3};
int length = sizeof array / sizeof array[0];
bubble_sort(array, length);
printf("\n");
return 0;
}
Output:
-1 1 2 2 3 3 4 5

Related

Swap is not done between 2 integers in array in C language

I'm implementing Quick Sort Algorithm in C language, in which at only one particular interchange of 2 values arr[i] and pivot is not being done properly, else all the swapping is done accurately, I have debugged too and tried to understand what is the problem/mistake, maybe it is a logical error.
Here's the code:
#include <stdio.h>
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void quickSort(int arr[], int low, int high)
{
int pvt = arr[high];
int i = low;
int j = high;
while (i < j)
{
while (pvt > arr[i])
i++;
while (pvt <= arr[j])
j--;
if (i < j)
swap(&arr[i], &arr[j]);
}
swap(&arr[i], &pvt);
printArr(arr, high + 1);
}
void main()
{
int arr[] = {10, 16, 8, 12, 15, 6, 3, 9, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
}
Just after 2-3 minutes, a friend of mine helped me out, that the pivot is not present in the array so with what I'm swapping it with arr[i]? Instead I can swap 2 values for the same result, arr[i] and arr[high]. Got my mistake :P

I'm having issues with getting MergeSort to work in C

I'm having issues with getting my mergeSort algorithm to work properly. The code is below, but I'll briefly summarize what I have tried and what I know is not wrong with the code.
The mergeSort function takes a pointer to an array and the array's size as arguments. If the size of the array is less than 2, it returns immediately. I'm sure that this works, because I debugged this part several times. It returns 8 times, which is what I expected it to do.
Next, a variable mid is created as an index to split the array by. I tested it, and I'm pretty confident that mid is correct across all recursions. Then, two arrays are created, the first containing the elements from the indexes 0...mid-1 and the second containing the elements from the indexes mid to n. Next, the sizes of each of these arrays are computed. I tested this too, and the sizes seem correct across all recursions as well. mergeSort is called on the left array and the right array, and then merge is called.
The actual merge function takes a pointer to the original array, a pointer to the left array, a pointer to the right array, and two integer values which are the sizes of each of the left and right arrays. Then, it compares the elements in L and R and copies the smaller of the two into A, in order.
My output, however, is [2] [4] [1] [6] [8] [5] [3] [7]
My expected output is: [1] [2] [3] [4] [5] [6] [7] [8]
I'm genuinely not sure why the code isn't working. There's probably something I'm overlooking, but I've been trying to solve it for an hour and figured I'd ask for help.
If you take the time to answer, or attempt to answer this question, thank you for your time.
My code is below:
#include <stdio.h>
#include <stdlib.h>
void mergeSort(int *, int);
void merge(int *, int *, int, int *, int);
void print(int *, int);
int main() {
int A[8] = { 2, 4, 1, 6, 8, 5, 3, 7 };
int arraySize = sizeof(A) / sizeof(A[0]);
mergeSort(A, arraySize);
print(A, arraySize);
}
void mergeSort(int *A, int n) {
if (n < 2) {
return;
}
int mid = n / 2;
int L[mid];
int R[n - mid];
for (int i = 0; i < mid; i++) {
L[i] = A[i];
}
for (int j = mid; j < n; j++) {
R[j - mid] = A[j + mid + 1];
}
int leftSize = sizeof(L) / sizeof(L[0]);
int rightSize = sizeof(R) / sizeof(R[0]);
mergeSort(L, leftSize);
mergeSort(R, rightSize);
merge(A, L, leftSize, R, rightSize);
}
void merge(int *A, int *L, int leftSize, int *R, int rightSize) {
int i, j, k;
while (i < leftSize && j < rightSize) {
if (L[i] < R[j]) {
A[k] = L[i];
k++;
i++;
} else {
A[k] = R[j];
k++;
j++;
}
}
}
void print(int *A, int n) {
for (int i = 0; i < n; i++) {
printf("[%d] ", A[i]);
}
printf("\n");
}
There are multiple problems:
the intialization loop for R is incorrect: you should copy A[j] instead of A[j + mid + 1]
the merge function should copy the remaining elements from the left or right arrays once the test i < leftSize && j < rightSize fails.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
void mergeSort(int *, int);
void merge(int *, int *, int, int *, int);
void print(const int *, int);
int main() {
int A[8] = { 2, 4, 1, 6, 8, 5, 3, 7 };
int arraySize = sizeof(A) / sizeof(A[0]);
mergeSort(A, arraySize);
print(A, arraySize);
return 0;
}
void mergeSort(int *A, int n) {
if (n < 2) {
return;
}
int mid = n / 2;
int L[mid];
int R[n - mid];
for (int i = 0; i < mid; i++) {
L[i] = A[i];
}
for (int j = mid; j < n; j++) {
R[j - mid] = A[j];
}
int leftSize = sizeof(L) / sizeof(L[0]);
int rightSize = sizeof(R) / sizeof(R[0]);
mergeSort(L, leftSize);
mergeSort(R, rightSize);
merge(A, L, leftSize, R, rightSize);
}
void merge(int *A, int *L, int leftSize, int *R, int rightSize) {
int i, j, k;
while (i < leftSize && j < rightSize) {
if (L[i] <= R[j]) {
A[k++] = L[i++];
} else {
A[k++] = R[j++];
}
}
while (i < leftSize) {
A[k++] = L[i++];
}
while (j < rightSize) {
A[k++] = R[j++];
}
}
void print(const int *A, int n) {
for (int i = 0; i < n; i++) {
printf("[%d] ", A[i]);
}
printf("\n");
}

Unable to sort Array while implementing QuickSort

I have implemented quicksort with 2 different approach taken in partition set
these are the programs
1.http://ideone.com/fPtQFT
2.http://ideone.com/KuXKr4
1.
int partition(int *a,int l,int r)
{
int i=l,pivot=l;
int j=l+1;
for(;j<r;j++)
{
if(a[j]<a[pivot])
{
swap(&a[i+1],&a[j]);
i++;
}
}
swap(&a[pivot],&a[i]);
return i;
}
2.
int partition(int *a,int l,int r)
{
int pivot=l;
int j=r-1;
int i=l+1;
while(1)
{
while(i<=j&&a[i]<=a[pivot])
i++;
while(i<=j&&a[j]>=a[pivot])
j--;
if(j<i)
break;
else
swap(&a[i],&a[j]);
}
swap(&a[pivot],&a[j]);
return j;
}
I am unable to figure out what's wrong in my code as the sorted output for example the test case
13 2 43 3 55 21 43 1 5 32
are wrong 1 2 3 13 21 32 42 5 43 55
Any help to figure out what's wrong in the logic of partitionset
The quicksort function takes the range in the form m to n where, m is the first element and n is one past the last element. This is done correctly in the main:
quicksort(a,0,N);
This means the second argument denotes the first element: 0, and the third argument denotes the one past the last element: N.
This is not done correctly in the first recursive call, where the last element is skipped because p-1 denotes the last element, instead of one past the last element:
quicksort(a,l,p-1);
Use this code to sort the array using quick sort Quick sort
QuickSort(A, P , R)
{
if(P < R)
{
Q = partition(A, P , R);
QuickSort(A, P, Q - 1);
QuickSort(A, Q + 1, R);
}
}
int partition(A, P , R)
{
while(1)
{
key = A[P];
i = P;
j = R;
while(key > A[i] && key != A[i])
i++;
while(key < A[j] && key != A[j])
j--;
if(i < j)
swap(A[i],A[j]);
else
return j;
}
}
this should do it:
#include <stdio.h>
#define N 10
void swap(int *a,int*b)
{
int temp=*b;
*b=*a;
*a=temp;
}
int partition(int *a,int l,int r)
{
int i=l,pivot=a[r];
int j=l;
for(;j<r;j++)
{
if(a[j]<pivot)
{
swap(&a[i],&a[j]);
i++;
}
}
swap(&a[i],&a[r]);
return i;
}
void quicksort(int *a,int l,int r){
int p;
if(l<r)
{
p=partition(a,l,r);
quicksort(a,l,p-1);
quicksort(a,p+1,r);
}
}
int main(){
int a[N] = {13, 2, 43, 3, 55, 21, 43, 1, 5, 32};
int i;
quicksort(a,0,N);
for(i=0;i<N;i++)
printf("%d ",a[i]);
return 0;
}

Quicksort implementation in C using first element as pivot

I am a complete beginner to stackoverflow and this is my first post. Please forgive if this is not the correct place to post these kinds of queries. I have written code for the Quicksort algorithm, based on the algorithm given in the Algorithms course in Coursera(It is not for any assignments though).
Basically, there are two functions Quicksort which is called recursively and partition() function that returns the index of the pivot. I select the pivot as the first element of the array every time. I checked the partition() function and it works fine but the array is not sorted even after I call the Quicksort() function.
Any help is appreciated. Thanks.
#include <stdio.h>
void swap(int *p, int i, int j)
{
int temp = *(p+i);
*(p+i) = *(p+j);
*(p+j) = temp;
}
int partition(int *q, int l, int r)
{
int i = l+1, j;
int p = l;
int len = r-l +1;
for (j = l+1; j < len; j++)
{
/*printf("%d \n", j);*/
if ( *(q+j) < *(q+p) )
{
swap(q, i, j);
i += 1;
}
}
swap(q, l, i-1);
/*printf("%d", i-1);*/
return (i-1);
}
void quicksort(int *ptr, int low, int high)
{
if (low < high)
{
int p = partition(ptr, low, high);
printf("%d\n", p);
quicksort(ptr, low, p);
quicksort(ptr, p+1, high);
}
}
int main(){
int i;
int a[] = {3, 8, 2, 5, 1, 4, 7, 6};
int len = sizeof(a)/sizeof(a[0]);
for ( i = 0; i < len; ++i)
{
printf("%d ", a[i]);
}
printf("\n");
int *ptr = a;
quicksort(ptr, 0, len-1);
for (i = 0; i < sizeof(a)/sizeof(a[0]); ++i)
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
2 corrections.
Small one: Change 3rd line inside if block in QuickSort function
from
quicksort(ptr, low, p);
to
quicksort(ptr, low, p-1);
This will improve performance.
Main error:
Your partition function is wrong. Specifically the loop where j runs from l+1 to r-l+1, because, r-l+1 can be less than l+1
I'll write the partition function for you if you want (post a comment if you face any problem with that) though I'd advice you to do it yourself.
EDIT:
A possible partition function:
int partition(int *q, int l, int r){
int i,j;
int p = *(q + l);
for(i = l + 1, j = r; ;){
while(*(q + i) <= p)
i++;
while(*(q + j) >= p)
j--;
if(i >= j)
break;
swap(q, i, j);
}
return i;
}
Changes noted in comments.
int partition(int *q, int l, int r)
{
int i = l+1, j;
int p = l;
/* fix: int len = r-l+1; is not used */
for (j = l+1; j <= r; j++) /* fix: j <= r */
{
if ( *(q+j) <= *(q+p) ) /* fix: <= */
{
swap(q, i, j);
i += 1;
}
}
swap(q, l, i-1);
return (i-1);
}
void quicksort(int *ptr, int low, int high)
{
if (low < high)
{
int p = partition(ptr, low, high);
quicksort(ptr, low, p-1); /* optimization: p-1 */
quicksort(ptr, p+1, high);
}
}
If interested, Hoare partition scheme is faster. If you switch to this, don't forget to change the two quicksort calls to quicksort(lo, p) and quicksort(p+1, hi) ). You might want to change the Hoare pivot to pivot = A[(lo+hi)/2], which will avoid worst case issue with sorted or reverse sorted array.
http://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme

Error in merge sort program

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

Resources