bubble sort in c


void bubbleSort(int arr[], int n){
   int i, j;
   for (i = 0; i < n-1; i++)      
  
       for (j = 0; j < n-i-1; j++) 
           if (arr[j] > arr[j+1])
              swap(&arr[j], &arr[j+1]);
}

4
8
A-312 69370 points

                                    #include &lt;stdio.h&gt;
#include &lt;stdbool.h&gt;


int main()
{
    int arr[] = { 12, 11, 13, 5, 6 };
    int arr_size = sizeof(arr) / sizeof(arr[0]);
    int i, j;
	bool isSwapped = false;
    
    for (i = 0; i &lt; arr_size; i++) {
        for (j = 0; j &lt; arr_size - i - 1; j++) {
            if (arr[j] &gt; arr[j + 1]) {
                // Swapping elements
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                
                isSwapped = true;
            }
        }
    }

    printf(&quot;Array After Sorted:\n&quot;);
    for (i = 0; i &lt; arr_size; i++)
        printf(&quot;%d &quot;, arr[i]);
    return 0;
}

4 (8 Votes)
0
4.33
6
Awgiedawgie 440220 points

                                    // below we have a simple C program for bubble sort
#include &lt;stdio.h&gt;

void bubbleSort(int arr[], int n)
{
    int i, j, temp, flag=0;
    for(i = 0; i &lt; n; i++)
    {
        for(j = 0; j &lt; n-i-1; j++)
        {
            // introducing a flag to monitor swapping
            if( arr[j] &gt; arr[j+1])
            {
                // swap the elements
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
                // if swapping happens update flag to 1
                flag = 1;
            } 
        }
        // if value of flag is zero after all the iterations of inner loop
        // then break out
        if(flag==0)
        {
            break;
        }
    }
    
    // print the sorted array
    printf(&quot;Sorted Array: &quot;);
    for(i = 0; i &lt; n; i++)
    {
        printf(&quot;%d  &quot;, arr[i]);
    }
}

int main()
{
    int arr[100], i, n, step, temp;
    // ask user for number of elements to be sorted
    printf(&quot;Enter the number of elements to be sorted: &quot;);
    scanf(&quot;%d&quot;, &amp;n);
    // input elements if the array
    for(i = 0; i &lt; n; i++)
    {
        printf(&quot;Enter element no. %d: &quot;, i+1);
        scanf(&quot;%d&quot;, &amp;arr[i]);
    }
    // call the function bubbleSort
    bubbleSort(arr, n);
    
    return 0;
}

4.33 (6 Votes)
0
4.5
4
A-312 69370 points

                                    func Sort(arr []int) []int {
	for i := 0; i &lt; len(arr)-1; i++ {
		for j := 0; j &lt; len(arr)-i-1; j++ {
			if arr[j] &gt; arr[j+1] {
				temp := arr[j]
				arr[j] = arr[j+1]
				arr[j+1] = temp
			}
		}
	}
	return arr
}

4.5 (4 Votes)
0
3.8
5
Awgiedawgie 440220 points

                                    #include &lt;bits/stdc++.h&gt; 
using namespace std; 
  
void swap(int *xp, int *yp)  
{  
    int temp = *xp;  
    *xp = *yp;  
    *yp = temp;  
}  
  
// A function to implement bubble sort  
void bubbleSort(int arr[], int n)  
{  
    int i, j;  
    for (i = 0; i &lt; n-1; i++)      
      
    // Last i elements are already in place  
    for (j = 0; j &lt; n-i-1; j++)  
        if (arr[j] &gt; arr[j+1])  
            swap(&amp;arr[j], &amp;arr[j+1]);  
}  
  
/* Function to print an array */
void printArray(int arr[], int size)  
{  
    int i;  
    for (i = 0; i &lt; size; i++)  
        cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;  
    cout &lt;&lt; endl;  
}  
  
// Driver code  
int main()  
{  
    int arr[] = {64, 34, 25, 12, 22, 11, 90};  
    int n = sizeof(arr)/sizeof(arr[0]);  
    bubbleSort(arr, n);  
    cout&lt;&lt;&quot;Sorted array: \n&quot;;  
    printArray(arr, n);  
    return 0;  
}

3.8 (5 Votes)
0
4
2
Phoenix Logan 186125 points

                                    public static void bubbleSort(int arr[])
{
	for (int i = 0; i &lt; arr.length; i++) //number of passes
    {
		//keeps track of positions per pass      
    	for (int j = 0; j &lt; (arr.length - 1 - i); j++) //Think you can add a -i to remove uneeded comparisons 
        {
          	//if left value is great than right value 
        	if (arr[j] &gt; arr[j + 1])
            {
              	//swap values
            	int temp = arr[j];
              	arr[j] = arr[j + 1];
              	arr[j + 1] = temp; 
            }
        }
    }
}

4 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
bubble sort sort when to use bubble sort bubblesort program in c bubble sort in java using functions bubble sort for array in c bubble sort program in c using function bubble sort algorithm for java c program for implementation of bubble sort c sorting bubble write a program in c to implement bubble sort c program to bubble sort explain bubble sort algorithm of bubble sort c bubble sort array program java program on bubble sort logic of bubble sort in java c bubblesort c code bubble sort bubble short in c bubble sort in descending order in c bubble sort in c ascending order bubble sort examples bubble sort in java explanation bubble sort best sorting using bubble sort in c what is bubble sort in java use c function for bubble sort code for bubblesort in c how to use bubble sort in c c bubble sor bubble sort problem ascending order in java Implementation of Bubble sort algorithm using c bubble sort in c using pointer bubble sort wiki why choose bubble sort java bubble sort algoritmo c booble sort c bubble sort in cobol program Bubble sort in COBOL use bubble sort in c write functions to perform to perform bubble sort in c bubble sort algorithm explanation sort. bubble sort when will you use bubble sort bubble sort program in java with time analysis bubble sort concept bubble soring in array java bubble sort iterations java bubble sort in java do while loop bubble sort anektode How is a bubble sort algorithm implemented? java bubble sort prgram in C write a c program to find the bubble sort steps bubble sorting an array in c bubble sort java code example step by step bubble sort code example step by step bubble sort algorithm explanation in C algorithm of bubble sorting in c bubble short in c language bubble sort on a structure in c algorithm for bubble sort c simple bubble sort program in c bubble sort algorithm Code masry bubble sort in c algorithm BUBBLE program in c concept of bubble sort BUBBLE SORT TECHNIQUE bubble sort algorithms bubble sorrt c bubble sort algorithmus bubble c buble sort en c Bubble Sort. implement bubble sort in c bubble sort with pointer in c sorting array in c using bubble sort bubble sort algorithm explained bubble sort function in java what is bubble sort used for Write a c program to sort an array using bubble sort method. bubble sort example step by step in java how to implement bubble sort simple bubble sort for array program in java simple bubble sort program in java algorithm for bubble sort in c how to code bubble sort in c bobble sort in c short description for bubble sort program bubble sort algorithm in c the simplest bubble sort type java bubble sort syntax syntax for bubble sort in java bubble sort array in java bubble sort in c using function sorting data by using bubble sort in java bubblesort code in c bubble sort in c langugae c buuble sort in c bubble sort in c bubble sort how organize bubble sort java bubble sort algorithm in c best case bubble sort jav stooge sort code java using bubble sort code for bubble sort decreasing in c code for bubble sort in c bubble_sort in C bubble sort online write a program to implement the bubble sort algorithm sorting elements of an array using bubble sort in c bubble sort theory steps in bubble sort bubble sorting al define bubble sort bubble sort c descending order how bubble sort algorithm works bubble sorting algorithms whats is bubble sort def\ bubble sort On bubble sort meaning bubble sort char c bubble sort c programize bubble sort in c code bubble sort in c language java how to do bubble sort bubble sort c example bubbele sort in c which sort is used in bubble sort buuble sort in c bubble sort method java String bubble sorting in c programming Bubble code what is bubble sort meant to do how to code in bubble bubble sort steps explain bubble sort algorithm with example bubbel sort in c how to define bubble sort code bubble sort algorithm in c bubble sort in c on tructure c program for bubble sort bubble sort implementation bubble sort program c bubble sort program bubble sort working bubble sort algorithmn bubble sort on java bubble sort descending order in c bubble sort you bubble sort al bubble sort how to use bubble sort bubble sort c Code Example bublble sort in c cs50 bubble sort Bubble and squeak bubble sort algorithm code how to do bubble sort bubble sort what is a compare implementation of bubble sort in c what is bubble sort algorithm the bubble sort is an easy way to arrange data in ascending order bubble sort algorithm code in c bubble sort in ascending order how to bubble sort why bubble sort called what is bubble sort in c bubble sort easy explanation bubble sort code using java why is bubble sort called bubble sort bubble sort in descending order c java bubble sort array bubble sort in C sort in c easiest bubble sort code bubble sort algorithmus java C in bubble bubble sort using functions how does bubble sort work How bubble sort works how to create bubble sort java bubble sort codice bubble sort loop\' bubble sort code using function in c How Bubble Sort Works? bubble sort search in c bubble sort javaa bubble sort java.. bubble sort java. bubble sort explanation list out operators in bubble sort bubble sort method bubble sort algorithm java example Write a program for bubble sort. bubble sort in java descending order bubble sort in c bubble sort explained Write a program to sort the data in ascending order using Bubble sort Algorithm bubble swap c bubble sort java program bubble sort for c Implement Selection , Bubbles and insertion sort on an array of integers c++. bubblesort examples in c how does bubble sort work in java program to show each step in bubble sort process bubble short sums implementing bubble sort in c c++ bubble sort how to do bubble sort in C program for bubble sort in c bubble sort with java bubble sort c program bubblesort in c bubble sort visualizer how to improve bubble sort bubble sort program c++ BUBLE SORT ALGO.IO bubble sort return type of array c code for bubble sort bubble sort for strings in c without string functions bubble sort for strings in c bubble sort real life example bubble sort using c he was asked to implement bubble sort void bubblesort java bubble sort method bubble sort integer java sorting code implementing bubble sort in java bubble sort logic bubble sort third iteration Sorting: Bubble Sort advanced bubble sort sort array using bubble sort bubble sort without introducing new variable bubble sort in c program error: static declaration of &lsquo;bubblesort&rsquo; follows non-static declaration void bubblesort(int arr[], int n) ^ bubble sort code in c bubble algorithm java bubel sort in c How to make a method that sorts an array of integers in descending order by the bubble sort method. sorrting in array bubble bable sort array bubble sort in c\ WAP in java to implement Bubble Sort Algo. in java int bubble sort bubble sort algoritm in java bubble sorting program in c Buuble sort C bubble sort algorithm What is bubble sort in Java? what is a bubble sort bubble sort loop\ time complexity of bubble sort buble sorting function cpp bubble sort numbers codetrain bubble sort c# bubble sort example of bubble sort in c++ function bubble sort c how to order an array from least character to greatest bubble sort java bubble sort algorithm definition bubble sor in c bubble no code swap sort algorithm bubble sort c++ bubble sort serial number java bubble sort array buble sort c bubble sort cormen java program of bubble sort how to bubble sort c bubble sort in data structrure geeks for geeks optimized bubble sort related : bubble sort bubble sort syntax searching using using bubble sort wap to find element in array using bubble sort buble sort for c Implementing bubble sort algorithm in a C program with worst-case execution time bubble sort function bubble sort example bubble sort code c++ bubble sort in program c bubble sorting c bubble sort bubble sort geeksforgeeks bubble sort program in c interchange sort in system programming bubble sort in javascript geeksforgeeks bubble sort using javascript geeksforgeek space complexity gfg of bubble sort how to calculate step number in bubble sort array to test bubble sort bubble sorting algorithm bubble sort analysis bubble sort fuhnction class in java bubble sort implementation in java bubble sort in c++ what is bubble sort BUBLE SORT ARRAY C++ buble sort coding example in c bubble search how to print bubble sort with only iteration Bubble sort implementation in c bubble sort algorithm bubble sort thrading java bubble sort function in c bubble sort algorithm c java array bubble sort how to bubble sort array in java bubble sort in c bubble sort program java buble short in c buble sort in c bubble sort javascript gfg BUBLLLE SORT CODE bubble sort algorithm in c Write a program to sort a list of integers using bubble sort technique what is a bubble sort in c bubblesort c bubble sort output place bubbleshort in c bubble sort c explanation descending bubble sort in c a function that uses bubble sort to sort an array from smallest to largest java bubblesort bubble sorting c bubble sort login bubble sort in o(n) bubble sort c code bubble sort en c sorting an array using bubble sort algorithm c bubble sort different solutions bubble sort ic c bouble sort in c buuble sort example bouble short inc genarate full bubble sort simulation bubble.is and java best case bubble sort bubble sort implemenation in java bubble sort array in descending order in c bubble sort best case bubble sort site:geeksforgeeks.org code for the bubble sort algorithm machine needs to sort using bubble sort bubble sort array in c bubble sorting in c Explain and write code of bubble sort bubble sort numbers C++ bubblesort example implement bubble sort java bubble sort in c by user input Bubble Sort will be better when data is in ____________ condition? bubble sort after loop optimization in compiler design bubble sort after loop optimization construction of basic blocks in bubble sort bubble sort series bubble sort algorithm c++ loop otimization of buuble sort in compiler design An array with n elements needs sorting using bubble sort. Which of the following gives the maximum number of swap(aj,aj+1) in order to sort the array elements using the given sorting algorithm? how to bubble sort an array in java buble sort in java time complexity of bubble sort, quick sort, finding items binary search tree java bubble sort implementation howto build a bubble sort program with a quit loop that uses user input howto build a bubble sort program with a quit loop What is the best and average case time complexity of bubble sort? bubbleSort in java where is bubble sort used bubble sort method java optimised bubble sort code for bubble sort bubble sort example in java bubble sort i bubble sort for unsorted ll cpp bubble sort least to greatest best case complexity of improve bubble sort where is bubble sort algorithm used] bubble sort algorithm in c++ java buble sort bubble sort implem bubble sort in java gfg code bubble sort c Write a program to implement bubble sort geeks for geeks bubble sort Sort the costumer&rsquo;s data by their account number. Use/write a Bubble Sort. passwise example of bubble sort bubble sort passwise output example bubble sory array how to use bubble sort in java c function for bubble sort swap method in java bubble sort bubble sort on array of integers in java Robin was sorting an array using bubble sort technique realise that no swap was made during the first pass How do you make a bubble sort more efficient code bubble sort algorithm java bubblesort in c++ hbubble sort c++ how to find the number of shopping needed to start a array by using bubble sort bubble sort with steps in java why does outer loop run only n- 1 time sin bubble sort bubblesort code example java bubblesort code example jav abubble sort code java bubble sort code Write code to implement Bubble Sort java bubble sort in array in c bubble sorting in java bubbles sort java bubble sort java code bubble sort code bubble sorting java bubble sort bubble search java Write a java program that performs the sorting of a group of integer values using bubble sort technique. bubblesort c++ sorting array in java using bubble sort swap sort java buble sort code implementation of bubble sort in java bubble sort in java by calling method bubble sort code Bubble sort. in java coding bubble sort java bubbel sort java bubbleSort java bubble sort in java program bubble sort c best java programs bubble sort bubble sort in java best example program java bubblesort array java bubble sort program bubblesort bubble sort with array java bubble sort javac# bubblesort code bubble sort in java sort array using bubble sort algorithm java bubble sort code java java code for bubble sort bubble sort array java bubble sorting array in java bubble sort program in java bubble sort algorithm in java bubble sort java java bubble sort java ubble sort bubble sort in java
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source