Mergesort implementation in C - c

Can someone please check my mergesort code. When I tried sorting the input values, the sorted array contained random values which weren't from input. And they were not sorted.
Is the way I'm declaring array within the while loop correct?
#include <stdio.h>
void merge (int a[], int aux[], int lo, int mid, int hi){
for(int y=lo; y<=hi; y++){
aux[y]=a[y];
}
int i=lo;
int j=mid+1;
for(int k=lo;k<=mid;k++){
if (j>hi) a[k]=aux[i++];
else if (i>mid) a[k]=aux[j++];
else if (a[j]<a[i])
a[k]= aux[j++];
else
a[k]=aux[i++];
}
return;
}
void sort (int b[],int aux[], int lo, int hi)
{
if(hi<=lo)
return;
int mid= lo+(hi-lo)/2;
sort(b, aux, mid+1, hi);
sort(b, aux, lo, mid);
merge(b,aux,lo,mid,hi);
return;
}
int main(void) {
int t,n;
long long int sum;
scanf("%d",&t);
while(t--){
sum=0;
scanf("%d",&n);
int w[n];
int m[n];
int g[n];
int h[n];
for (int i=0; i<n;i++){
scanf("%d",&m[i]);
}
for (int j=0; j<n;j++){
scanf("%d",&w[j]);
}
sort(w,g,0,n-1);
sort(m,h,0,n-1);
}
return 0;
}

You are compareing meaningless values in the merge funtion and stop the merging in the middle of the processs.
To correct them, in the merge function
Change k<=mid to k<=hi
Change a[j]<a[i] to aux[j]<aux[i]

Related

memory read failed for 0x4e00000000 at the end of the code when array's numbers are randomly generated

So I'm trying to apply (in C) randomly generated numbers to an array which will be sorted with Quick sort. There is no problem with generating numbers and sorting, but at the end of the code I have an error telling me there was a problem with memory reading.
#include <stdlib.h>
#include <time.h>
void generate(int AR[], int n){
srand((unsigned int) time(NULL));
for(int i=0;i<n;i++){
int num = (rand()%100);
AR[i]=num;
}
}// end of generate
void swap(int *a, int *b){
int temp=*a;
*a=*b;
*b=temp;
} //end of swap
int sort(int AR[], int beg, int end){
int pivot= AR[end];
int a=(beg-1);
for(int i=beg; i<=end-1; i++){
if(AR[i]<pivot){
a++;
swap(&AR[i], &AR[a]);
}//end of if
} //end of i<end-1
swap(&AR[a+1], &AR[end]);
return (a+1);
} //end of sort
void Quick_sort(int AR[], int beg, int end){
if(beg<end){
int placed_PV=sort(AR, beg, end);
Quick_sort(AR, beg, placed_PV-1);
Quick_sort(AR, placed_PV+1, end);
} //end of if beg<end
}//end of Quick sort
int main(){
int AR[]={};
int n=10;
generate(AR, n);
printf("\n");
printf("Nieposortowana tablica:");
for(int i=0; i<n;i++)
printf("%d,", AR[i]);
printf("\n");
Quick_sort(AR, 0, n-1);
printf("Posortowana tablica:");
for(int i=0;i<n;i++)
printf("%d,", AR[i]);
printf("\n");
} //end of code
error message is
error: memory read failed for 0x4e00000000
Thread 1: EXC_BAD_ACCESS (code=1, address=0x4e00000000)
Where is the mistake I've made?
you have this:
int AR[]={};
int n=10;
which is wrong because this array AR has only one element(due to this initialization) ,so you can't access ten elements of it.
fix like this
int AR[10]={0};

How to implement radix sort to work with negative numbers using my counting sort code

I'm trying to use my counting sort algorithm to implement a radix sorting algorithm.(counting already works with neg numbers).
void counting_sortrdx(int *vet, int mmax, int min, int n, int dig){
int i,j, C[10],B[n];
for (i=0;i<10;i++){
C[i]=0;
}
for (i=0;i<n;i++){
C[((vet[i]-min)/dig)%10]++;
}
for (i=1;i<10;i++){
C[i]=C[i]+C[i-1];
}
for (i=n-1;i>=0;i--){
B[C[((vet[i]-min)/dig)%10]-1]=vet[i];
C[((vet[i]-min)/dig)%10]--;
}
for (i=0;i<tam;i++){
vet[i]=B[i];
}
}
void radix_sort(int *vet, int max, int min, int n){
int dig, i;
for (dig = 1; max/dig > 0; dig=dig*10){
counting_sortrdx(vet, max, min, n, dig);
}
for (i=0;i<n;i++){
printf("%d ",vet[i]);
}
}

Language C - QuickSort

I was trying to code a quick sort program in c, and my recursive function is in an infinite loop.
The problem is:
You have been given an array
A of size
N.This array contains integers ranging from
1 to
10^9. You need to sort the contents of this array by their value and then print the contents of it.
Input Format:
The first line contains a single integers N denoting the size of the array. The next line contains N space separated integers denoting the contents of the array.
Output Format:
Print N space separated integers, i.e. the final sorted array.
Constraints:
1≤N≤10^6
1≤A[i]≤10^9
void fill_vet(unsigned int a[], unsigned int n){
int i;
for(i=0; i<n; i++)
scanf("%u", &a[i]);
}
void print_vet(unsigned int a[], unsigned int n){
int i;
for(i=0; i<n; i++)
printf("%u ", a[i]);
}
int partition(unsigned int a[], int start, int end){
int i, j;
unsigned int pivot, n_swap;
i = start + 1;
pivot = a[start];
for(j = start+1; j<=end; j++){
if(a[j] < pivot){
n_swap = a[j];
a[j] = a[i];
a[i] = n_swap;
i++;
}
}
n_swap = a[i-1];
a[i-1] = a[start];
a[start] = n_swap;
return i-1;
}
void sort_array(unsigned int a[], int start, int end){
while(start < end){
int pos_piv = partition(a, start, end);
sort_array(a, start, pos_piv-1);
sort_array(a, pos_piv+1, end);
}
}
int main()
{
unsigned int n, a[100000];
scanf("%u", &n);
fill_vet(a, n);
sort_array(a, 0, n-1);
print_vet(a, n);
return 0;
}
Can you tell me where is the error pls!
You are using recursion so you don't need a loop:
void sort_array(unsigned int a[], int start, int end){
if(start < end){
int pos_piv = partition(a, start, end);
sort_array(a, start, pos_piv-1);
sort_array(a, pos_piv+1, end);
}
}

Bubble Sort Algorithm in C

The program I"m trying to finish is a program using the bubble sort algorithm. I am not sure what is the problem or in which function the problem is in. The problem is the program does not sort the array in properly. (It also must be arranged in ascending order).
Here is the code:
#include <stdio.h>
#include "simpio.h"
void getArray (int arr[], int size);
void sortArray (int arr[], int size);
void swap (int arr[], int num, int number);
void dispArray (int arr[], int size);
bool checkBigger (int arr[], int num, int number);
main()
{
int size;
printf("Enter number of elements: ");
size=GetInteger();
int arr[size];
getArray(arr, size);
sortArray(arr, size);
dispArray(arr, size);
getchar();
}
void getArray (int arr[], int size)
{
int num;
printf("Please enter the value of the elements: \n");
for(num=0; num<size; num++)
{
arr[num]=GetInteger();
}
}
void sortArray (int arr[], int size)
{
int num, number, d;
for(num=0;num<size-1;num++)
{
for(d=0; d<size-num-1; d++)
{
number=num+1;
checkBigger(arr, num, number);
}
}
}
void swap (int arr[], int num, int number)
{
int tem;
tem=arr[num];
arr[num]=arr[number];
arr[number]=tem;
}
void dispArray (int arr[], int size)
{
int num;
printf("The sorted list is:\n");
for(num=0; num<size; num++)
{
printf("%d\t", arr[num]);
}
}
bool checkBigger (int arr[], int num, int number)
{
if(arr[num]>arr[number])
{
swap(arr, num, number);
}
}
Thank you very much.
void sortArray (int arr[], int size)
{
int num, number, d;
for(num=0;num<size-1;num++)
{
for(d=0; d<size-num-1; d++)
{
number=d+1;
checkBigger(arr, d, number);
}
}
}
pretty sure your problem is with you algorithm, try to simulate your algorithm in pen and paper. it will help your understanding of your code and the algorithm better :)
for your convenience here i am including a bubble sort algorithm i did some while ago
void bubbleSort( int a[], int n)
{
int i,j,temp; // for a={1,2,3,4,5} n is 5
n = n - 1; // bcz otherwise it will get out of index
for(i=0; i<n; i++)
{
for(j=0; j<n-i; j++)
{
if(a[j]>a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}
i hope this helps
All I follow from the above examples is an implementation of the exchange sort.
The exchange sort on the outer loop checks each entry in the table against the first element, exchanging when necessary. At then end of the inner loop, the lowest element is in position 1, then it begins with position 2, comparing it to the remaining elements, and doing an exchange. Even if the array was already in order, the sort cannot stop. It has to do a n*(n-1) compares. An array of 50 elements, already sorted will do 50*49 comparisons.
The bubble sort works differently
set a swap flag to zero. Then
slide along the array, comparing position(i) to position(i+1). If a swap takes place, you do the sort again.
here is some pseudo code.
swap = 0
do {
for (i=o;i< no-elements-1;i++) {
if (array[i] > array[i+1])
{
do the exchange
set swap=1
}
/**/
} while (swap == 1);
The above illustrates the bubble sort.
Note. if the data is in order, there is no swap and there is no second loop. The sort algorithm is able to quit early.
if a fifty element array is in order, the sort would have done 50 comparisons and would have stopped.
The exchange sort, which is described earlier would have to do 50*49 or 2450 comparisons.
// BUBBLE SORT.
#include <stdio.h>
#define MAX 20
int main()
{
int arr[MAX];int no;
printf("PLEASE ENTER THE CURRENT SIZE OF THE ARRAY\n");
scanf("%d",&no);
int i;
printf("PLEASE ENTER THE ELEMENTS OF THE ARRAY\n");
for(i=0;i<no;i++)
scanf("%d",&arr[i]); /*reading the elements*/
/* sorting begins*/
int j,k,l;
int temp;
int flag=0;
for(k=0;k<no-1;k++)
{
flag=0;
j=k;
for(i=0;i<no-j-1;i++) /* not going to the part that has been sorted*/
{
if(arr[i]>arr[i+1])
{
flag=1;
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
else
continue;/* not necessary*/
}
if(flag==0) /*implies that the array is alraedy sorted*/
break;
}
printf("THE SORTED LIST:\n\n");
for(i=0;i<no;i++)
printf("%d\n",arr[i]);
}

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