bubble sort program in c using function

Solutions on MaxInterview for bubble sort program in c using function by the best coders in the world

showing results for - "bubble sort program in c using function"
Lara
07 Jun 2016
1#include <bits/stdc++.h> 
2using namespace std; 
3  
4void swap(int *xp, int *yp)  
5{  
6    int temp = *xp;  
7    *xp = *yp;  
8    *yp = temp;  
9}  
10  
11// A function to implement bubble sort  
12void bubbleSort(int arr[], int n)  
13{  
14    int i, j;  
15    for (i = 0; i < n-1; i++)      
16      
17    // Last i elements are already in place  
18    for (j = 0; j < n-i-1; j++)  
19        if (arr[j] > arr[j+1])  
20            swap(&arr[j], &arr[j+1]);  
21}  
22  
23/* Function to print an array */
24void printArray(int arr[], int size)  
25{  
26    int i;  
27    for (i = 0; i < size; i++)  
28        cout << arr[i] << " ";  
29    cout << endl;  
30}  
31  
32// Driver code  
33int main()  
34{  
35    int arr[] = {64, 34, 25, 12, 22, 11, 90};  
36    int n = sizeof(arr)/sizeof(arr[0]);  
37    bubbleSort(arr, n);  
38    cout<<"Sorted array: \n";  
39    printArray(arr, n);  
40    return 0;  
41}
Leah
16 Jul 2018
1// below we have a simple C program for bubble sort
2#include <stdio.h>
3
4void bubbleSort(int arr[], int n)
5{
6    int i, j, temp, flag=0;
7    for(i = 0; i < n; i++)
8    {
9        for(j = 0; j < n-i-1; j++)
10        {
11            // introducing a flag to monitor swapping
12            if( arr[j] > arr[j+1])
13            {
14                // swap the elements
15                temp = arr[j];
16                arr[j] = arr[j+1];
17                arr[j+1] = temp;
18                // if swapping happens update flag to 1
19                flag = 1;
20            } 
21        }
22        // if value of flag is zero after all the iterations of inner loop
23        // then break out
24        if(flag==0)
25        {
26            break;
27        }
28    }
29    
30    // print the sorted array
31    printf("Sorted Array: ");
32    for(i = 0; i < n; i++)
33    {
34        printf("%d  ", arr[i]);
35    }
36}
37
38int main()
39{
40    int arr[100], i, n, step, temp;
41    // ask user for number of elements to be sorted
42    printf("Enter the number of elements to be sorted: ");
43    scanf("%d", &n);
44    // input elements if the array
45    for(i = 0; i < n; i++)
46    {
47        printf("Enter element no. %d: ", i+1);
48        scanf("%d", &arr[i]);
49    }
50    // call the function bubbleSort
51    bubbleSort(arr, n);
52    
53    return 0;
54}
Odele
03 Mar 2018
1algorithm bubble sort
queries leading to this page
bubble sort explainedbubble sorting in c programmingprogram to show each step in bubble sort processbubble sort c programbubble sort in c codebubble sort implementation in cc in bubble sortbubble sort arraybubble sort in c 5cbuble sort in cbubble sort in c using function and array with stringsbubble sorting in array in cbubble sort code in cuse c function for bubble sorthe was asked to implement bubble sortbubble sort using cbubble sort program in csorting codebubbleshort in cbubble sort algorithm explanation in cimplementation of bubble sort algorithm using cc program for bubble sortbubble sort algorithm c in c bubble sortc 2b 2b bubble sortwhat is bubble sorting in cwrite a c program to find the bubble sort stepsbubble sort in c programbubble program in calgorithm for bubble sort in cbubble sorting cbubble sort for cbubble sort for descending order in cbubble sort codebubble sort algorithm in cin c bubble sortcode for bubblesort in cbuble sort for cbubllle sort codebubblesort program in cbubble sort cimplement bubble sort in cbubble sort algorithm c 2b 2bbubble sort in c using functionbubblesort codebubble sorting in cbublble sort in csorting an array of numbers using bubble sort in ccode for the bubble sort algorithmimplementing bubble sort in calgorithm for bubble sort cbable sort arraybubble sort in program cbubble sort program in c with algorithmbubble sort c 2b 2bbubble sorting program in cbubble sort descending order in cbubble sort in c languagebubble sort output placec function for bubble sortbuuble sort cbubble sort numbersc bubble sortbubble sort in c ascending ordersorting 3a bubble sortbubble sort algorithm in c the simplestbubble sort in c what is nbubble sort c code examplewrite functions to perform to perform bubble sort in cbubble sort functionsimple bubble sort program in cbubblesort c 2b 2bbubble sort algorithm code in cc bubble sort programbubble short sumsarray to test bubble sortbubble sort for strings in c without string functionsbubble sor in cbuble short in cbubble sort for array in cbubble sort prgram in cbubble sortingsorting using bubble sort in cbubble sort algoritmo chow to use bubble sort in cbubble sort code using function in cwrite a program in c to implement bubble sortbubble sort program cbubble sort in c using string using function bubble in cbubble sort program c 2b 2bbubble sort in c sort in cbuble sort codebubble sorting algorithmwhat is bubble sort in cbubble sort in descending order chow to make bubble sort with pass c languageexample of bubble sort in cbubble sort passwise output examplebubble sort examplea function that uses bubble sort to sort an array from smallest to largestinterchange sort in system programmingbubble sort site 3ageeksforgeeks orgbubble sorting an array in cc bubble sort array programuse bubble sort in cbubble sort program in c tutorialspointbubble sort in cbuble sort carray of structures bubble sort chow to do bubble sort in cbubble sort c descending orderbubble sort search in cbubble sort function in cimplementation of bubble sort in cbuble sort coding example in chow to make bubble sort c language with pass bubble sort c examplebubble sort in o 28n 29sorting an array using bubble sort algorithmbubblesort cc in bubblebubble sort in cbubble sort codebubblesort in calgorithm of bubble sorting in chbubble sort c 2b 2bbubble sort in c on tructurec bubblesortbubble sorting array in cbubble sort array in cbubble sort program in c using functionbubble sort for strings in cbubble cbubble sory arrayc program for implementation of bubble sortbubble sort algorithm in cc code for bubble sortbubble sort c codebubble sort en cbubble sort program in c using function