Something is wrong with my bubble sort output - c

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);
}

Related

Bubble sort, print array per pass

How can you print the array per iteration in Bubble sort?
So far this is my sort function and im not really sure about it:
void sortBubble(int *arr)
{
int i, j;
int temp;
for (i = 0; i < SIZE; i++)
{
for (j = i + 1; j < SIZE; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printArr(arr); //call print function to print sorted array
}
If you want something to happen on each iteration of a loop, it should be placed within the loop body.
void sortBubble(int *arr)
{
int i, j;
int temp;
for (i = 0; i < SIZE; i++)
{
for (j = i + 1; j < SIZE; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
printArr(arr);
}
}
It's also advisable to minimize the scope of variables. i, j, and temp do not need to be scoped at the function level.
void sortBubble(int *arr)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = i + 1; j < SIZE; j++)
{
if (arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
printArr(arr);
}
}
One final note: you can optimize your bubble sort by keeping track of the number of swaps performed in the inner loop. If zero swaps are performed you know the array is already sorted and you can immediately return.
Bubble sort is O(n^2) in the worst case. Your implementation is always O(n^2). This small change can reduce it to O(n) in the best case scenario, or in the realistic scenario, somewhere in between.
void sortBubble(int *arr)
{
for (int i = 0; i < SIZE; i++)
{
int swaps = 0;
for (int j = i + 1; j < SIZE; j++)
{
if (arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
swaps++;
}
}
printArr(arr);
if (swaps == 0 /* or !swaps */) return;
}
}

arrange the array in an order such that the even values are followed by odd values in the array

this is the program I made ,if I input [13,11,10,17,18] i get the output [12,13,17,11,10]. I do not understand what mistake I am making. somebody please help me understand.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n,j,i,num,v;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Entered number of elements: %d\n", n);
ptr = (int*)malloc(n * sizeof(int));
for (i = 0; i < n; ++i) {
scanf("%d",&v);
ptr[i] = v;
}
i=0;
j=0;
while(i<5){
j++;
if (ptr[j]%2==0 && i%2==0){
num=ptr[i];
ptr[i]=ptr[j];
ptr[j]=num;
}
if (ptr[j]%2!=0 && i%2 !=0){
num=ptr[i];
ptr[i]=ptr[j];
ptr[j]=num;
}
if (j==4){
i++;
j=0;
}
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
Ok, I tried to tell you how you should have written you program, but you didn't listen:
Make a https://stackoverflow.com/help/minimal-reproducible-example
A MCVE needs all the includes
No interactive stuff. You need to run and run and run your program in a debugger. You don't want to put data in manually every single time.
You want many tests, and you want to repeat them, so that when you fix one, you don't break another.
Make a function which does the job.
Free your memory!
Now to the solution: your idea of a solution was fine apart from the stuff about indexes. It's pretty similar to the one you will find down here. The only difference is that I put the odd numbers al the end to avoid checking elements multiple times.
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
void evenodd(int *v, size_t n)
{
for (size_t i = 0; i < n; ++i) {
while (i < n && v[i] % 2 == 0) {
++i;
}
--n;
while (i < n && v[n] % 2) {
--n;
}
if (i < n) {
int tmp = v[i];
v[i] = v[n];
v[n] = tmp;
}
}
}
bool is_evenodd(int *v, size_t n)
{
size_t i = 0;
while (i < n && v[i] % 2 == 0) {
++i;
}
while (i < n && v[i] % 2 != 0) {
++i;
}
return i == n;
}
void main_test(const int *v, size_t n)
{
int *v1 = memcpy(malloc(n * sizeof(int)), v, n * sizeof(int));
evenodd(v1, n);
if (is_evenodd(v1, n)) {
printf("Ok!\n");
}
else {
printf("Fail!\n");
}
free(v1);
}
int main(void)
{
main_test((int[]) { 1 }, 0);
main_test((int[]) { 1 }, 1);
main_test((int[]) { 2 }, 1);
main_test((int[]) { 1, 2 }, 2);
main_test((int[]) { 1, 3 }, 2);
main_test((int[]) { 2, 1 }, 2);
main_test((int[]) { 2, 4 }, 2);
main_test((int[]) { 1, 3, 2 }, 3);
main_test((int[]) { 1, 4, 2 }, 3);
size_t n = 1000;
int *a = malloc(n * sizeof *a);
for (size_t i = 0; i < n; ++i) {
a[i] = rand();
}
main_test(a, n);
free(a);
return 0;
}
You can try the following code :
int even_index = 0; //start index
int odd_index = 4; //end index
for(int i=0;i<5;i++){
if(ptr[i] % 2 == 0){
int temp = ptr[even_index];
ptr[even_index++] = ptr[i]; //swapping values and incrementing even_index
ptr[i] = temp;
}else{
int temp = ptr[odd_index];
ptr[odd_index--] = ptr[i];
ptr[i] = temp;
}
}
or you can also count the number of even numbers in the digits during input and assign odd_value = even_num // number of even digits
ok so I solved it....
see the even numbers always end up in even indexes so we need to set a pointer on those even index(current index) and search for any even number after the current index.
if we find any(even number) we swap the current index value with the even number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n,j,i,num,v;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Entered number of elements: %d\n", n);
ptr = (int*)malloc(n * sizeof(int));
for (i = 0; i < n; ++i) {
scanf("%d", &v);
ptr[i] = v;
}
i=0;
j=0;
while(i<n && j<n){
if (ptr[j]%2==0){
num=ptr[i];
ptr[i]=ptr[j];
ptr[j]=num;
i+=2;
j=i;
}
j++;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
If this problem is to sort an array in order (descending) and then further place all even values before odd values I would recommend:
Sort the array
Swap and shift any odd numbers with the even numbers
Here's a naive implementation:
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (arr[i] < arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
int lastEven = 0;
for (int i = 0; i < len - 1; i++) {
if (arr[i] % 2 && (arr[i + 1] % 2 == 0)) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
lastEven = i;
} else if (arr[i] % 2 == 0 && lastEven-i > 1) {
for (int j = i; j > lastEven; j--) {
tmp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = tmp;
}
lastEven++;
}
}
Given the input [13,11,10,17,18] this will first sort the array ([18,17,13,11,10]) then separate the evens and odds ([18,10,17,13,11])

Reverse Bubble Sort Algorithm in C using only while loop

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.

C - Remove Duplicates from an Array

I'm quite new to programming, I wrote a code to remove duplicates from an array, logically, it should work, but it doesn't.... I logically tested it multiple times and it made sense...
Here's the code:
#include <stdio.h>
int rmDuplicates(int arr[], int n)
{
int i, j;
for (i = 0; i < n; i++) {
if (arr[i] == arr[i + 1]) {
for (j = i + 1; j < n - 1; j++) {
arr[j] = arr[j + 1];
}
n--;
}
return n;
}
}
int main()
{
int n, i;
scanf("%d", &n);
int arr[n];
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
n = rmDuplicates(arr, n);
for (i = 0; i < n; i++) {
printf("%d", arr[i]);
}
printf("\n%d", n);
return 0;
}
Your "return n" is in the wrong place, and returns after the first cycle.
for(i=0;i<n;i++) {
if(arr[i] == arr[i+1]) {
for(j=i+1;j<n-1;j++) {
arr[j] = arr[j+1];
}
n--;
}
return n; // <---- this
}
// <-- should be here.
As confirmation, if I move the return n; outside the loop, the code works. But it only removes consecutive duplicates, because you only check arr[i] against its consecutive, arr[i+1].
(Also, the cycle ought to stop at n-1, because otherwise arr[n-1+1] is arr[n] which is outside the array).
A final issue is that if you have, say,
n
...5,..., 5, 5, 6
i j
and you check the first 5 against the second, and find it a duplicate, then shift all that follows by one step, in the j-th position you will have a 5 again, but j will now be incremented and you will test the first 5 against the 6 instead of the third 5, not finding the duplicate:
n
...5,..., 5, 6
i j
For this reason, when you find a match, you need to rewind j by one and repeat that test:
int rmDuplicates(int arr[], int n) {
int i,j,k;
for (i=0;i<n-1;i++) {
for (j=i+1; j < n; j++) {
if(arr[i] == arr[j]) {
n--;
for (k=j;k<n;k++) {
arr[k] = arr[k+1];
}
j--;
}
}
}
return n;
}
From a performance point of view, the above algorithm is O(n^2), that is, if the array list doubles, the algorithm takes four times as long; if it trebles, it takes nine times as long.
A better algorithm would therefore be to first sort the array in-place, so that 1 3 2 7 2 3 5 becomes 1 2 2 3 3 5 7 (this has a cost of O(n log n), which grows more slowly ); then you just "compress" the array skipping duplicates, which is O(n) and gets you 1 2 3 5 7
int i, j;
for (i = 0, j = 1; j < n;) {
if (arr[i] == arr[j]) {
j++;
continue;
}
i++;
if (j != (i+1)) {
arr[i] = arr[j];
}
j++;
}
n = i+1;
size_t removeDups(int *arr, size_t size)
{
if(arr && size > 1)
{
for(size_t current = 0; current < size - 1; current++)
{
size_t original_size = size;
size_t copypos = current + 1;
for(size_t cpos = current + 1; cpos < original_size; cpos++)
{
if(arr[current] == arr[cpos])
{
if(cpos < original_size -1)
{
if(arr[current] != arr[cpos + 1])
{
arr[copypos++] = arr[cpos + 1];
cpos++;
}
}
size--;
}
else
{
arr[copypos++] = arr[cpos];
}
}
}
}
return size;
}
int main(void)
{
int arr[] = {1,1,1,2,2,3,3,4,5,6,7,1,8,8,2,2,2,2};
size_t size = sizeof(arr) / sizeof(arr[0]);
size = removeDups(arr, size);
for(size_t index = 0; index < size; index++)
{
printf("%d\n", arr[index]);
}
}

Strange Bubble sort behaviour

Can anyone explain why this bubble sort function doesn't work and why I lose numbers in my output? I'm very new to C, so please forgive me if this is something very obvious I have missed.
#include <stdio.h>
#include <stdlib.h>
int bubble(int array[],int length) {
int i, j;
int temp;
for(i = 0; i < (length); ++i) {
for(j = 0; j < (length - 1); ++j) {
if(array[i] > array[i+1]) {
temp = array[i+1];
array[i+1] = array[i];
array[i] = temp;
}
}
}
return 0;
}
int main() {
int array[] = {12,234,3452,5643,0};
int i;
int length;
length = (sizeof(array)/sizeof(int));
printf("Size of array = %d\n", length);
bubble(array, length);
for (i = 0; i < (length); ++i) {
printf("%d\n", array[i]);
}
return 0;
}
Output
Size of array = 5
12
234
3452
0
0
In your inner loop, you don't use j at all. Double check your logic.
Also note that array[i+1] goes beyond the array boundary.
for (i = 0; i < (length-1); ++i) {
for (j = 0; j < (length-i-1); ++j) {
if(array[j] > array[j+1]) {
temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
In a bubble sort you only use the inner loop variable.
Another thing, the inner loop goes from 0 to i if I remember well; but I think that's just an optimisation (as the tail is remains sorted in each step).
Try to run step by step your code with paper and pencil. That always works.
for (i = 0; i < (length); i++) {
for (j = 1; j < (length-i); j++) {
if(array[j-1] > array[j]) {
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
}
}

Resources