Bubble Sort in C - c

I am trying to implement Bubble sort in C and have come this far but its nor sorting properly.
#include<stdio.h>
int main()
{
int n, i, j, a[5], b, temp;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n+1; ++j)
{
if(a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
Input
2
12
1
13
Output
2
1
12
13
What am I missing ?

So now that the drama is behind us, the problem with your code was that you were not using the proper index in your inner loop. In addition your inner loop counter's conditional check was not correct. Also, as I mentioned in a comment to your question, you have a flaw in your code (which I have not fixed) where you initialize your array prior to asking the user how many elements they want to enter. This can lead to an index out of bounds exception if the user enters a number greater than 5.
#include<stdio.h>
int main()
{
int n, i, j, a[5], b, temp;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n-1; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}

your second loop is not proper.
for(j=0;j<n-i-1;j++){
}

/*By your approach , in inner loop you are not checking the each elements . So change i to j in swapping , and limit of j should be till n-1*/
#include<stdio.h>
int main()
{
int n, i, j, a[10], b, temp=0;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n-1; ++j) // notice limit , also complexity can be reduced by changing to(j<n-i-1)
{
if(a[j] > a[j+1])
{
temp = a[j]; // changed variable
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}

/*You May check this code it will help you*/
#include<stdio.h>
void bubble_sort(int a[],int n);
void bubble_sort(int a[],int n)
{
int i,j;
int temp;
for(i=0;i<n;i++)
{
for (j=0; j<=n-i-1;j++)
{
if (a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
main()
{
int a[10];
int i,n;
printf("Enter the number\n");
scanf("%d",&n);
printf("Enter the number\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble_sort(a,n);
printf("sorted elements are\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}

#include<stdio.h>
int main()
{
int n, i, j, a[5], b, temp;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
if(a[j] > a[j+1]) //change the varible instead of i to j
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}

I've tried to cover all possible conditions to reduce passes and comparisons for bubble sort to reduce the overall time taken. Here goes my code...
#include <stdio.h>
#include <conio.h>
void bubbleSort(int n){
int arr[n],i,j,temp=0;
int swapFlag = 0;
printf("\nInsert %d elements:\n",n);
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("Insert complete.\n\n");
printf("Your array looks like:\n");
for(i=0;i<n;i++){
printf("%d ",arr[i]);
}
//Bubble Sort Algorithm
for(i=0;i<n-1;i++){
swapFlag = 0;
for(j=0;j<n-i-1;j++){
if(arr[j]>arr[j+1]){
swapFlag = 1;
//Swapping unordered pairs
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
//Condition to reduce number of passes & comparisons
if(swapFlag == 0){
break;
}
}
printf("\n\nAfter sorting the array looks like:\n");
for(i=0;i<n;i++){
printf("%d ",arr[i]);
}
}
void main(){
int n;
printf("Enter number of array elements: ");
scanf("%d",&n);
bubbleSort(n);
getch();
}
Result:-

Related

How can I move all elements in an array with a K number and make the numbers rotate when it comes to the end?

#include <stdio.h>
#include <stdlib.h>
int main()
{
int N, K, i;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Please enter value of K: ");
scanf("%d", &K);
int arr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < N; i++)
{
int temp = arr[i];
arr[i + K] = arr[i];
arr[i] = temp;
}
for (i = 0; i < N; i++)
{
printf("%d", arr[i]);
}
return 0;
}
I know the current code is totally wrong It was just another test.
Basically what I need to do is something like this:
the array before: arr[N]={1,2,3,4,5,6,7,8,9,10}
the array after if K is 2: arr[N]={10,9,1,2,3,4,5,6,7,8}
Like #whozcraig pointed out for in-place rotation of array members.
Define a function to reverse(in-place) array members in a range :
static inline void
arr_reverse (const int start, const int end, int arr[]) {
for (int ai = start, zi = end; ai < zi; ++ai, --zi) {
int tmp = arr[ai];
arr[ai] = arr[zi];
arr[zi] = tmp;
}
}
Then you call it like :
K %= N;
if (K != 0) {
arr_reverse (0, N-1, arr);
arr_reverse (0, K-1, arr);
arr_reverse (K, N-1, arr);
}
I write something like this but it creates new array instead of modifying current one but it works.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int N, K, i;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Please enter value of K: ");
scanf("%d", &K);
int arr[N], newArr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < K; i++)
{
newArr[i] = arr[N-1-i];
}
for (int x = 0; i < N; i++)
{
newArr[i] = arr[x];
x++;
}
for (i = 0; i < N; i++)
{
printf("%d ", newArr[i]);
}
return 0;
}
Thank you guys, for all the comments and answers. I've tried them and they worked.
I have found a way to do it as well. It's bit different. I did it with a function which moves all the elements with 1 position and then repeat the func as much as needed (K times).
#Cheatah helped me come up with it. I will post it in case somebody likes this solution in the future.
Here it is:
#include <stdio.h>
#include <stdlib.h>
int moveOnePos(int num, int array[num])
{
int temp = array[num - 1];
for (int b = num - 1; b > 0; b--)
{
array[b] = array[b - 1];
}
array[0] = temp;
return 0;
}
int main()
{
int N, K, i;
printf("Please enter size of array: ");
scanf("%d", &N);
printf("Please enter K: ");
scanf("%d", &K);
int arr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < K; i++)
{
moveOnePos(N, arr);
}
for (i = 0; i < N; i++)
{
printf("%d", arr[i]);
}
return 0;
}

Selection sort in C

Is following C code is written according to selection sort algorithm? I'm little bit confused about it. However, it gives correct results.
#include <stdio.h>
int main() {
int n;
printf("Enter size of array: ");
scanf("%d", &n);
int a[n], i = 0, j, min;
while (i < n) {
printf("Enter no. %d:", i + 1);
scanf("%d", &a[i]);
i++;
}
for (i = 0; i < n; i++) {
for (j = i, min = a[i]; j < n; j++) {
if (a[j] < min)
min = a[j];
else
continue;
a[j] = a[i];
a[i] = min;
}
}
for (i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
Selection Sort
#include<stdio.h>
int main() {
int array[100], n, c, d, position, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}

Transpose of matrix is sending Error in C

There are two 2D arrays and r and c are their row and column, i and j are used for integer variables.
Transpose of matrix is running well till the value r and c are entered.
#include <stdio.h>
int main() {
// your code goes here
int a[50][50], b[50][50], i, j, r, c;
printf("Enter the value of R and c");
scanf("%d%d", &r, &c);
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
b[i][j] = a[i][j];
}
}
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("%d\n", b[i][j]);
}
printf("\n");
}
return 0;
}
You are not "transposing" the matrix. You are simply copying one array to another. To transpose a matrix, you need to convert rows to columns:
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
b[j][i]=a[i][j];
}
}
and reverse c and r in the loop condition when printing:
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d\n",b[i][j]);
}
printf("\n");
}
Note that your arrays are capable of handling 50x50. So you need to ensure the input values of r and c don't exceed these limits.
Your code is
#include <stdio.h>
int main() {
// your code goes here
int a[50][50], b[50][50], i, j, r, c;
printf("Enter the value of R and c\n");
scanf("%d%d", &r, &c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
printf("\n");
for(i = 0; i < c; i++)
{
for(j = 0; j < r; j++)
{
printf("%3d", a[j][i]);
}
printf("\n");
}
return 0;
}

How to delete duplicated values in array in C?

I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
You should start by describing your problem in pseudo code, and breaking it into smaller pieces.
Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:
for each element in inputArray
if not elementIsDuplicate(element)
add to outputArray
That's a single for loop. The elementIsDuplicate is separate function.
Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.
#include<stdio.h>
int main(){
int array[50], i, j, k=0, c, n, array2[50] = {0};
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; ++i){
int num, dup = 0;
printf("array[%d]= ", i); scanf("%d", &num);
for(j = 0; j < k; ++j){
if(array[j] == num){
array2[j] = dup = 1;
break;
}
}
if(!dup){
array[k++] = num;
}
}
for (c=i=0; i < k; ++i){
if(!array2[i])
printf("%d ", array[c++] = array[i]);
}
printf("\n");
/*
for(i=0;i<c;++i)
printf("%d ", array[i]);
printf("\n");
*/
system("pause");
return 0;
}
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //enter array elements
for(c=0;c<n;c++)
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\n",b[c]);
return 0;
}
This will remove multiple duplicates from the desired array..
Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9

C array nondecreasing order insert value

I am writing a program that arranges an array in nondecreasing order; then, it inserts a value into the sequence. I can easily get numbers in the beginning and the middle of the array, but whenever I add a number that should go at the end, I keep getting 0. Where am I going wrong?
#include <stdio.h>
int main()
{
int array[10];
int i, j, n, m, temp, key, pos;
printf("Enter number of elements:\n");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements:\n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf("Sorted list is\n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
printf("Enter the element to be inserted X:\n");
scanf("%d", &key);
for (i = 0; ; i++)
{
if (key < array[i])
{
pos = i;
break;
}
}
m = n - pos + 1 ;
for (i = 0; i <= m; i++)
{
array[n - i + 2] = array[n - i + 1] ;
}
array[pos] = key;
printf("Final list is:\n");
for (i = 0; i < n + 1; i++)
{
printf("%d\n", array[i]);
}
}
If the number to be entered is larger than all elements the following loop poses an issue.
...
for (i = 0; ; i++)
{
if (key < array[i])
{
pos = i;
break;
}
}
If the number is largest,then the pos will be junk and not n.The default value of an array is junk.
Replace it with this.
for (i = 0;i<n ; i++)
{
if (key < array[i])
{
break;
}
}
pos = i;
...

Resources