I was trying to make a functional bucket sort array in c with a variable funtion parameter in order to sort each bucket, so I tried to pass the bubblesort algorithm but it doesn't seems to do anything with each bucket, my question is: What am I doing wrong?, at my first glance it seems that there aren't mistakes in my code: (sorry for the mess)
void bubblesort (int arr[], unsigned char size) {
size /= sizeof(int);
for (unsigned char i = 0; i < size - 1; i++)
for (unsigned char j = 0; j < size - i - 1; j++)
if (arr[j] > arr[j+1])
arr[j] = arr[j] + arr[j+1],
arr[j+1] = arr[j] - arr[j+1],
arr[j] = arr[j] - arr[j+1];
}
/* More sorting algorithms */
void bucketsort (int arr[], unsigned char size, unsigned char n, void sort(int[],unsigned char)) {
// to be fixed
size /= sizeof(int); int *bucket[n]; unsigned char s[n];
for (unsigned char i = 0; i < n; i++) s[i] = 0, bucket[i] = NULL;
int min = arr[0], max = arr[0];
for (unsigned char i = 1; i < size; i++) {
if (arr[i] < min) min = arr[i];
if (arr[i] > max) max = arr[i];
} for (unsigned char i = 0; i < size; i++)
s[n*(arr[i]-min)/(max-min+1)]++,
bucket[n*(arr[i]-min)/(max-min+1)] = (int*)realloc(bucket[n*(arr[i]-min)/(max-min+1)],s[n*(arr[i]-min)/(max-min+1)]*sizeof(int)),
bucket[n*(arr[i]-min)/(max-min+1)][s[n*(arr[i]-min)/(max-min+1)]-1] = arr[i];
for (int i = 0; i < n; i++) {for (int j = 0; j < s[i]; j++) printf("%d ",bucket[i][j]); printf("\n");}
for (unsigned char i = 0; i < n; i++) sort(bucket[i],s[i]), fflush(NULL);
for (int i = 0, index = 0; i < n; i++) for (int j = 0; j < s[i]; j++) arr[index++] = bucket[i][j];
}
int main (void) {
int a[] = {3,6,4,9,1,7,5,8,2,0};
bucketsort(a,sizeof(a),4,bubblesort);
for (signed char i = 0; i < (signed char)(sizeof(a)/sizeof(int)); i++) printf("%d, ",a[i]);
}
In my function parameter I´ve tried replacing the [] with * but it doesn't make any difference.
You have to remove the line "size /= sizeof(int)" - and the size is correct
void bubblesort (int arr[], unsigned char size) {
// size /= sizeof(int);
for (unsigned char i = 0; i < size - 1; i++)
for (unsigned char j = 0; j < size - i - 1; j++)
if (arr[j] > arr[j+1])
arr[j] = arr[j] + arr[j+1],
arr[j+1] = arr[j] - arr[j+1],
arr[j] = arr[j] - arr[j+1];
}
Test:
1 2 0
3 4
6 7 5
9 8
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ~"[Thread 26424.0x564c exited with code 0]
Related
This is just a normal bubble sort code, but something is wrong in it.
#include <stdio.h>
#include <stdlib.h>
void bubble_sort(int size, int *arr);
int main(void)
{
int *array, size;
printf("enter the amount of data: ");
scanf("%d", &size);
array = (int*)malloc(sizeof(int) * size);
for (int i = 0; i < size; ++i)
{
scanf("%d", &array[i]);
}
bubble_sort(size, array);
for (int i = 0; i <= size; ++i)
{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
void bubble_sort(int size, int *arr)
{
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
if (arr[j] > arr[j + 1])
{
int temp;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
else if (arr[j] == arr[j + 1])
{
continue;
}
}
}
}
When I execute it, a zero appear in the output when it must be print 1 2 3
[my_username#my_computer bubble_sort]$ ./bubble_sort
enter the amount of data: 3
3 1 2
0 1 2 3
^
|
Where are this zero came from?
Well, it is not quite Bubble Sort.
To start, as pointed out in the comments: arr[j + 1] accesses the array with an invalid index - one past the end of the array when j is at its maximum value (size - 1).
The loop to print the array
for(int i = 0; i <= size; ++i)
has a similar off-by-one error. i < size is the desired condition.
Accessing an array out-of-bounds invokes Undefined Behaviour.
The else if block within the sort is superfluous.
The simplified variant of bubble sort is usually written as:
void bubble_sort(int *array, size_t length)
{
for (size_t i = 0; i < length; i++) {
for (size_t j = i + 1; j < length; j++) {
if (array[i] > array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
Whereas the standard bubble sort stops if it makes a pass on the array without swapping.
void bubble_sort(int *array, size_t length)
{
int swapped;
do {
swapped = 0;
for (size_t i = 1; i < length; i++) {
if (array[i - 1] > array[i]) {
int temp = array[i - 1];
array[i - 1] = array[i];
array[i] = temp;
swapped = 1;
}
}
} while (swapped);
}
This can be optimized by observing that all elements after the last swap have already been sorted.
void bubble_sort(int *array, size_t length)
{
do {
size_t n = 0;
for (size_t i = 1; i < length; i++) {
if (array[i - 1] > array[i]) {
int temp = array[i - 1];
array[i - 1] = array[i];
array[i] = temp;
n = i;
}
}
length = n;
} while (length);
}
I am learning dsa and I tried to implement radix sort in c. I used this tutorial from programiz.
I implemented it but the array of numbers they are using get sorted correctly but when I used a different array it shows some garbage value.
When I insert this
int array[] = {121, 432, 564, 23, 1, 45, 788};
The output is "1 23 45 121 432 564 788" output
But When I insert this
int array[] = {3,4,1,5,2};
The output is "1 2 3 4 2496"
output
I implemented the code as it is from the tutorial. Can any one point out the issue
#include <stdio.h>
void radix_sort(int array[], int size);
void counting_sort(int array[], int size, int place);
int main(void)
{
int array[] = {3,4,1,5,2};
int size = sizeof(array) / sizeof(int);
radix_sort(array, size);
for (int i = 0; i < size; i++)
printf("%i ", array[i]);
printf("\n");
}
void radix_sort(int array[], int size)
{
int max = array[0];
for (int i = 1; i < size; i++)
{
if (array[i] > max)
max = array[i];
}
for (int place = 1; max / place > 0; place *= 10)
counting_sort(array, size, place);
}
void counting_sort(int array[], int size, int place)
{
int output[size + 1];
int max = (array[0] / place) % 10;
for (int i = 1; i < size; i++)
{
if (((array[i] / place) % 10) > max)
max = array[i];
}
int count[max+1];
for (int i = 0; i < max; ++i)
count[i] = 0;
for (int i = 0; i < size; i++)
count[(array[i] / place) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
for (int i = size-1; i >= 0; i--)
{
output[count[(array[i] / place) % 10] - 1] = array[i];
count[(array[i] / place) % 10]--;
}
for (int i = 0; i < size; i++)
array[i] = output[i];
}
for (int i = 1; i < 10; i++) { will access the array out of bounds since count[max+1] => count[5+1] so your program has undefined behavior. It could crash or output garbage or do just about anything.
You should use i <= max in that loop:
for (int i = 1; i <= max; i++) // use `i <= max` here
count[i] += count[i - 1];
You also need to zero out the memory of the variable length array count because it will contain indeterminate values after it's been created. You tried doing that with for (int i = 0; i < max; ++i) count[i] = 0; but that misses the last element in the array. You need i <= max there too:
int count[max + 1];
for (int i = 0; i <= max; ++i) count[i] = 0;
I'm learning C and my bubble sort code returns a random number sometimes (although sometimes it does returns the correct array numbers).
For example, it should print {0, 1, 3, 10}. But sometimes prints things like {-487420160, 0, 1, 3} or {-1484260208, 0, 1, 3} (only the first number changes, the other are correct).
This is the code:
#include <stdio.h>
int main(){
//array and variables
int arr[] = {1, 3, 0, 10};
int len = sizeof(arr)/ sizeof(int);
int j = 0;
int i = 0;
//print original array
printf("ORIGINAL ARRAY: \n");
printf("{");
for (int x = 0; x < len; x ++){
printf("%d", arr[x]);
if (x < len-1){
printf(", ");
}
}
printf("}\n");
//sorting
for ( i = 0;i < len; i++){
for (j = 0 ;j < len; j ++){
if(arr[j] > arr[j+1]){
int temporal = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temporal;
}
}
}
//print sorted array
printf("BUBBLE-SORTED ARRAY: \n");
printf("{");
for (int y = 0; y < len; y ++){
printf("%d", arr[y]);
if (y < len-1){
printf(", ");
}
}
printf("}\n");
return 0;
}
Do anybody know why or can help me improve the code?
In place of the for(j=0:j<len;j++) change to for(j=0:j<len-1;j++):
for ( i = 0;i < len; i++){
for (j = 0 ;j < len; j ++){// here instead of len use len-1
if(arr[j] > arr[j+1]){
int temporal = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temporal;
}
}
}
Now, in bubble sort we usually compare until the end, but in your case when j=len then j+1 is a random number which has no existence and there is no element.
Make the following change and you will get the solution.
#include <stdio.h>
int main(){
//array and variables
int arr[] = {1, 3, 0, 10};
int len = sizeof(arr)/ sizeof(int);
int j = 0;
int i = 0;
int x=0;
int y=0;
//print original array
printf("ORIGINAL ARRAY: \n");
printf("{");
for (x = 0; x < len; x ++){
printf("%d", arr[x]);
if (x < len-1){
printf(", ");
}
}
printf("}\n");
//sorting
for ( i = 0;i < len; i++){
for (j = 0 ;j < len-1; j++){// Here is the change, len has been changed to len-1
if(arr[j] > arr[j+1]){
int temporal = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temporal;
}
}
}
//print sorted array
printf("BUBBLE-SORTED ARRAY: \n");
printf("{");
for (y = 0; y < len; y ++){
printf("%d", arr[y]);
if (y < len-1){
printf(", ");
}
}
printf("}\n");
return 0;
}
I am trying to create a reverse bubble sort algorithm. It works but the first output is a big crazy number that i dont understand. The remaining outputs seem to be sorted in descending order. Where is my code wrong?
#include <stdio.h>
void ft_rev_int_tab(int *tab, int size)
{
int i;
int j;
int k;
i = 0;
while (i < size)
{
j = 0;
while (j < size -i)
{
if (tab[j] < tab[j+1])
{
k = tab[j];
tab[j] = tab[j+1];
tab[j + 1] = k;
}
j++;
}
i++;
}
}
int main(void)
{
int tab[] = {9, 320, 0, 113, 15};
int size = sizeof(tab) / sizeof(*tab);
ft_rev_int_tab(tab, size);
for (int i = 0; i < size; i++)
{
printf ("%d\n", tab[i]);
}
}
i = 0; before while (i < size) is wrong. The condition j < size -i is equivalent to j < size when i is zero. In this case j will be at most size-1 and now tab[j+1] is out-of-range.
It should be changed to i = 1;.
void ft_rev_int_tab(int *tab, int size) {
int i;
int j;
int k;
i = 0;
while (i < size) {
j = 0;
while (j < size - i - 1) {
if (tab[j] < tab[j + 1]) {
k = tab[j];
tab[j] = tab[j + 1];
tab[j + 1] = k;
}
j++;
}
i++;
}
}
Your condition in the nested while loop should be (j < size - i - 1)
because you only need to check the value from tab[0] to tab[i-1]. Your if and swap checks tab[j] and tab[j+1], so when i is equal to the size of the array, you will compare it with a value outside of the array. In your example is the tab[5]'s value.
I'm coding a function in C to check if an array has any duplicate value, and if so replace it by any of the non-present ones. The array consists of numbers shuffled from 1 to Nmax:
unsigned char * NotRepeated (unsigned char *arr){
unsigned char i, j, count, k, notrepeated[Nmax];
k = 0;
for (i = 0; i < Nmax ; i++){
count = 0;
for (j = 0; j < Nmax; j++){
if (arr[j] != i + 1){
count++;
}
if (count == Nmax){
notrepeated[k] = i + 1;
k++;
}
}
}
k = 0;
for (i = 0; i < Nmax - 1; i++){
for (j = i + 1; j < Nmax; j++){
if (arr[i] == arr[j]){
arr[j] = notrepeated[k];
k++;
}
}
}
return arr
}
If I print the array notrepeated[k] I get almost all the array filled when the original array arr[j] seldomly has more than 2 repeated figures.
What am I doing wrong?
Thanks!