{"id":113720,"date":"2026-06-07T15:00:45","date_gmt":"2026-06-07T09:30:45","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=113720"},"modified":"2026-06-07T15:00:49","modified_gmt":"2026-06-07T09:30:49","slug":"bubble-sort-in-c","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/bubble-sort-in-c\/","title":{"rendered":"Bubble Sort in C: A Complete Beginner\u2019s Guide"},"content":{"rendered":"\n<p>Imagine you have a pile of unsorted cards on a table. You want to arrange them in order. One simple approach is to compare adjacent cards and swap them if they are in the wrong order. You repeat this process until all cards are sorted. This is exactly how bubble sort works.<\/p>\n\n\n\n<p>Bubble sort is one of the simplest sorting algorithms and one of the best ways to learn how sorting works. Even though it is not the fastest algorithm for large datasets, it is perfect for beginners because the logic is easy to understand. You compare two items, swap them if needed, and repeat until everything is sorted.<\/p>\n\n\n\n<p>If you are learning to program in C, studying data structures, or trying to understand sorting algorithms, bubble sort is the perfect starting point. It teaches you fundamental concepts like loops, comparisons, and array manipulation.<\/p>\n\n\n\n<p>This guide explains what bubble sort in C is, how the algorithm works step by step, and shows you complete C code examples you can run and modify.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Quick TL;DR Summary<\/strong><\/h2>\n\n\n\n<ol>\n<li>This guide explains bubble sort, a simple sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order until the entire array is sorted.<br><\/li>\n\n\n\n<li>You will learn how bubble sort works through step-by-step examples, why it is called bubble sort, and how the algorithm keeps making passes through the data until no more swaps are needed.<br><\/li>\n\n\n\n<li>The guide covers complete C code implementations including the basic version and optimized versions that stop early when the array is already sorted.<br><\/li>\n\n\n\n<li>You will understand the time and space complexity of bubble sort, how to analyze its performance, and when bubble sort is actually useful despite being slower than other algorithms.<br><\/li>\n\n\n\n<li>You will see practical examples of sorting numbers and strings using bubble sort in C, and learn how to modify the code for different scenarios.<\/li>\n<\/ol>\n\n\n\n<div class=\"guvi-answer-card\" style=\"margin: 40px 0;\">\n\n  <div style=\"\n    position: relative;\n    background: linear-gradient(135deg, #f0fff4, #e6f7ee);\n    border: 1px solid #cfeedd;\n    padding: 26px 24px 22px 24px;\n    border-radius: 14px;\n    font-family: Arial, sans-serif;\n    box-shadow: 0 6px 16px rgba(0,0,0,0.05);\n  \">\n\n    <!-- Top accent -->\n    <div style=\"\n      position: absolute;\n      top: 0;\n      left: 0;\n      height: 6px;\n      width: 100%;\n      background: linear-gradient(to right, #099f4e, #6dd5a3);\n      border-radius: 14px 14px 0 0;\n    \"><\/div>\n\n    <!-- Title -->\n    <h3 style=\"\n      margin: 10px 0 12px 0;\n      color: #099f4e;\n      font-size: 20px;\n    \">\n      What Is Bubble Sort?\n    <\/h3>\n\n    <!-- Content -->\n    <p style=\"\n      margin: 0;\n      color: #2f4f3f;\n      font-size: 16px;\n      line-height: 1.7;\n    \">\n      Bubble sort is a simple sorting algorithm that repeatedly steps through a list, compares adjacent elements, and swaps them if they are in the wrong order. Each pass through the list pushes the largest unsorted element to its correct position, similar to bubbles rising to the surface\u2014hence the name. The process continues until no swaps are needed, indicating that the list is fully sorted. Although easy to understand and implement, bubble sort is inefficient for large datasets compared to more advanced sorting algorithms.\n    <\/p>\n\n  <\/div>\n\n<\/div>\n\n\n\n<p>The algorithm gets its name because smaller elements &#8220;bubble&#8221; to the top (beginning) of the list with each pass, similar to how bubbles rise to the surface of water.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/hub\/data-structures-and-algorithms-tutorial\/bubble-sort-algorithm\/\" target=\"_blank\" rel=\"noreferrer noopener\">Bubble sort<\/a> is one of the easiest <a href=\"https:\/\/www.guvi.in\/hub\/data-structures-and-algorithms-tutorial\/introduction-to-sorting-algorithms\/\" target=\"_blank\" rel=\"noreferrer noopener\">sorting algorithms<\/a> to understand and implement. It requires no extra space beyond the array itself. However, it is slow for large datasets compared to algorithms like quicksort or mergesort.<\/p>\n\n\n\n<p><strong>Read More: <\/strong><a href=\"https:\/\/www.guvi.in\/blog\/sorting-in-data-structure-categories-types\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Sorting in Data Structure: Categories &amp; Types<\/strong><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How Bubble Sort Works<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Compare the first two elements<\/strong><\/h3>\n\n\n\n<p>Start at the beginning of the <a href=\"https:\/\/www.guvi.in\/hub\/c-tutorial\/arrays\/\">array<\/a>. Look at the first element and the second element. Compare them to see which is larger.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Swap if needed<\/strong><\/h3>\n\n\n\n<p>If the first element is larger than the second, swap them. The larger element moves forward. If the first element is smaller, leave them as they are.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Move to the next pair<\/strong><\/h3>\n\n\n\n<p>Move to the next pair of adjacent elements (second and third). Compare them and swap if needed. Continue this process through the entire array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 4: Complete one pass<\/strong><\/h3>\n\n\n\n<p>After comparing and swapping all adjacent pairs, you have completed one pass through the array. The largest unsorted element is now in its correct position at the end.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 5: Repeat until sorted<\/strong><\/h3>\n\n\n\n<p>Do another pass through the array, but this time stop one position earlier because you know the last position is already correct. Continue making passes, reducing the range each time, until no swaps occur in a complete pass.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why the name bubble sort?<\/strong><\/h3>\n\n\n\n<p>After each pass, the largest unsorted element moves to its correct position like a bubble rising through water. With each pass, more elements settle into their correct positions. Eventually, all elements are sorted.<\/p>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px;\">\n  <strong style=\"font-size: 22px; color: #FFFFFF;\">\ud83d\udca1 Did You Know?<\/strong>\n  <p style=\"margin-top: 14px; margin-bottom: 0;\">\n    <strong style=\"color: #FFFFFF;\">Bubble sort<\/strong> is one of the earliest and simplest sorting algorithms introduced in computer science education. It works by repeatedly comparing adjacent elements and swapping them if they are in the wrong order, causing smaller elements to gradually \u201cbubble\u201d toward the beginning of the list\u2014hence its name, sometimes also referred to as <strong style=\"color: #FFFFFF;\">\u201csinking sort\u201d<\/strong>. Although it is inefficient for large datasets due to its <strong style=\"color: #FFFFFF;\">O(n\u00b2)<\/strong> time complexity, bubble sort remains widely taught because it provides a clear and intuitive introduction to fundamental algorithmic concepts such as iteration, comparison, and in-place sorting, serving as a stepping stone to more advanced sorting techniques.\n  <\/p>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Simple Bubble Sort in C<\/strong><\/h2>\n\n\n\n<ol>\n<li><strong>Complete C code for basic bubble sort<\/strong><\/li>\n<\/ol>\n\n\n\n<p>#include &lt;stdio.h&gt;<\/p>\n\n\n\n<p>void bubbleSort(int array[], int size) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Outer loop for the number of passes<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; size &#8211; 1; i++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Inner loop for comparing adjacent elements<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 0; j &lt; size &#8211; i &#8211; 1; j++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Compare adjacent elements<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (array[j] &gt; array[j + 1]) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Swap if left is larger than right<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int temp = array[j];<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[j] = array[j + 1];<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[j + 1] = temp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>void printArray(int array[], int size) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; size; i++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;%d &#8220;, array[i]);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;\\n&#8221;);<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>int main() {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;int array[] = {64, 34, 25, 12, 22, 11, 90};<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;int size = 7;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;Original array: &#8220;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printArray(array, size);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;bubbleSort(array, size);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;Sorted array: &#8220;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printArray(array, size);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;return 0;<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<ol start=\"2\">\n<li><strong>How the code works<\/strong><\/li>\n<\/ol>\n\n\n\n<p>The bubbleSort function takes an array and its size as parameters. The outer loop controls how many passes through the array you make. The inner loop compares adjacent elements and swaps them if the left element is larger.<\/p>\n\n\n\n<p>The swap is done using a temporary variable. You store the first value in temp, move the second value to the first position, and then place the temp value in the second position.<\/p>\n\n\n\n<ol start=\"3\">\n<li><strong>Output of the program<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Original array: 64 34 25 12 22 11 90<\/p>\n\n\n\n<p>Sorted array: 11 12 22 25 34 64 90<\/p>\n\n\n\n<ol start=\"4\">\n<li><strong>Understanding the pass process<\/strong><\/li>\n<\/ol>\n\n\n\n<p>In the first pass, the largest element (90) bubbles to the end. In the second pass, the second-largest element (64) settles into position. With each pass, one more element finds its correct location. After six passes (size &#8211; 1), all seven elements are sorted.<\/p>\n\n\n\n<p><em>Did you know? Bubble sort is sometimes used ironically in programming challenges as a joke or difficulty test. This actually teaches valuable lessons about algorithm efficiency and why choosing the right algorithm matters. Most experienced developers avoid bubble sort in production code.&nbsp;<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Optimized Bubble Sort<\/strong><\/h2>\n\n\n\n<ol>\n<li><strong>The problem with basic bubble sort<\/strong><\/li>\n<\/ol>\n\n\n\n<p>The basic version makes the same number of passes regardless of whether the array becomes sorted early. If your array is already sorted, the basic version still does all the passes and comparisons.<\/p>\n\n\n\n<ol start=\"2\">\n<li><strong>Optimized version with early stopping<\/strong><\/li>\n<\/ol>\n\n\n\n<p>#include &lt;stdio.h&gt;<\/p>\n\n\n\n<p>void optimizedBubbleSort(int array[], int size) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Outer loop for passes<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; size &#8211; 1; i++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int swapped = 0;&nbsp; \/\/ Flag to check if swap occurred<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Inner loop for comparing adjacent elements<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 0; j &lt; size &#8211; i &#8211; 1; j++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (array[j] &gt; array[j + 1]) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Swap elements<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int temp = array[j];<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[j] = array[j + 1];<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[j + 1] = temp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;swapped = 1;&nbsp; \/\/ Mark that a swap happened<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ If no swap occurred, array is already sorted<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (swapped == 0) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp; \/\/ Exit early<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>void printArray(int array[], int size) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; size; i++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;%d &#8220;, array[i]);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;\\n&#8221;);<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>int main() {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;int array[] = {11, 12, 22, 25, 34, 64, 90};<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;int size = 7;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;Original array: &#8220;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printArray(array, size);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;optimizedBubbleSort(array, size);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;Sorted array: &#8220;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printArray(array, size);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;return 0;<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<ol start=\"3\">\n<li><strong>How optimization helps<\/strong><\/li>\n<\/ol>\n\n\n\n<p>The optimized version uses a flag called swapped to track whether any swap occurred during a pass. If a complete pass happens with no swaps, it means the array is already sorted. The <a href=\"https:\/\/www.guvi.in\/blog\/what-is-an-algorithm\/\">algorithm<\/a> stops immediately instead of continuing through remaining passes.<\/p>\n\n\n\n<p>For nearly sorted arrays, this optimization saves significant time. For already sorted arrays, the algorithm completes in just one pass instead of multiple passes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Bubble Sort for Sorting Strings<\/strong><\/h2>\n\n\n\n<ol>\n<li><strong>C code to sort strings alphabetically<\/strong><\/li>\n<\/ol>\n\n\n\n<p>#include &lt;stdio.h&gt;<\/p>\n\n\n\n<p>#include &lt;string.h&gt;<\/p>\n\n\n\n<p>void bubbleSortStrings(char words[][50], int size) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Outer loop for passes<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; size &#8211; 1; i++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Inner loop for comparing adjacent strings<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 0; j &lt; size &#8211; i &#8211; 1; j++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Compare strings using strcmp<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (strcmp(words[j], words[j + 1]) &gt; 0) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Swap strings if first is alphabetically after second<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char temp[50];<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(temp, words[j]);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(words[j], words[j + 1]);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(words[j + 1], temp);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>void printStrings(char words[][50], int size) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; size; i++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;%s &#8220;, words[i]);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;\\n&#8221;);<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>int main() {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;char words[5][50] = {&#8220;zebra&#8221;, &#8220;apple&#8221;, &#8220;mango&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;};<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;int size = 5;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;Original words: &#8220;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printStrings(words, size);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;bubbleSortStrings(words, size);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;Sorted words: &#8220;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;printStrings(words, size);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;return 0;<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<ol start=\"2\">\n<li><strong>Key differences for sorting strings<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Instead of comparing numbers with &gt;, you use strcmp() function to compare strings. The strcmp() function returns a positive value if the first string comes alphabetically after the second string.<\/p>\n\n\n\n<p>Use strcpy() to copy string values instead of simple variable assignment. You cannot assign strings directly in C, so you use string copy functions for swapping.<\/p>\n\n\n\n<ol start=\"3\">\n<li><strong>Output<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Original words: zebra apple mango banana cherry<\/p>\n\n\n\n<p>Sorted words: apple banana cherry mango zebra<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Time Complexity of Bubble Sort<\/strong><\/h2>\n\n\n\n<ol>\n<li><strong>Best case: O(n)<\/strong><\/li>\n<\/ol>\n\n\n\n<p>If the array is already sorted, the optimized version completes in one pass with no swaps. Time complexity is O(n) because you make one pass through n elements. This is the fastest possible performance for bubble sort.<\/p>\n\n\n\n<ol start=\"2\">\n<li><strong>Average case: O(n\u00b2)<\/strong><\/li>\n<\/ol>\n\n\n\n<p>For random data, bubble sort makes approximately n\u00b2\/2 comparisons and swaps. This simplifies to O(n\u00b2). If you have 100 elements, expect roughly 5000 operations.<\/p>\n\n\n\n<ol start=\"3\">\n<li><strong>Worst case: O(n\u00b2)<\/strong><\/li>\n<\/ol>\n\n\n\n<p>If the array is sorted in reverse order, bubble sort needs the maximum number of passes and comparisons. It still makes approximately n\u00b2\/2 operations, which is O(n\u00b2).<\/p>\n\n\n\n<ol start=\"4\">\n<li><strong>Space complexity: O(1)<\/strong><\/li>\n<\/ol>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Bubble_sort\" target=\"_blank\" rel=\"noopener\">Bubble sort<\/a> requires only a small amount of extra space for the temporary variable during swaps. It does not need extra arrays or data structures. Space complexity is O(1), meaning constant space regardless of input size.<\/p>\n\n\n\n<ol start=\"5\">\n<li><strong>Why bubble sort is slow<\/strong><\/li>\n<\/ol>\n\n\n\n<p>For large datasets, O(n\u00b2) becomes very slow. For 10,000 elements, you need about 50 million operations. For 100,000 elements, you need about 5 billion operations. Faster algorithms like quicksort with O(n log n) are much better for large datasets.<\/p>\n\n\n\n<p>To learn more about Pointers in C, enroll in this <strong>HCL GUVI\u2019s <\/strong><a href=\"https:\/\/www.guvi.in\/courses\/programming\/c-programming-for-beginners\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=bubble-sort-in-c-a-complete-beginners-guide\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>C Programming course <\/strong><\/a>designed for beginners and aspiring developers. The course covers essential C programming concepts including pointers, memory management, arrays, functions, file handling, loops, operators, and more through hands-on practice and real-world examples. With self-paced learning, expert guidance, and an industry-recognized <strong>NSDC certification<\/strong>, this course helps you strengthen your programming fundamentals and coding skills.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Bubble sort is one of the simplest sorting algorithms and perfect for beginners learning to code. It repeatedly compares adjacent elements and swaps them until the entire array is sorted.<\/p>\n\n\n\n<p>The algorithm is easy to understand with clear logic. You compare, swap if needed, and repeat. Despite being slow for large datasets with O(n\u00b2) time complexity, bubble sort teaches important programming concepts.<\/p>\n\n\n\n<p>The optimized version with early stopping is much better than the basic version. It detects when the array is sorted and stops immediately, saving unnecessary passes.<\/p>\n\n\n\n<p>Bubble sort is best used for learning, small datasets, or nearly sorted data. For large datasets in production code, use faster algorithms like quicksort or mergesort.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1780333343332\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">1. <strong>Why is bubble sort called bubble sort?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The algorithm gets its name because larger elements &#8220;bubble&#8221; to their correct positions at the end of the array with each pass, similar to how bubbles rise through water. After each pass, one more element settles into its final sorted position.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1780333349413\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">2. <strong>What is the time complexity of bubble sort?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Best case is O(n) for already sorted data using optimized version. Average and worst cases are O(n\u00b2). This means for 1000 elements, you need about 1 million comparisons in the worst case. Faster algorithms like quicksort use O(n log n) time.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1780333358687\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">3. <strong>Is bubble sort used in real applications?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Bubble sort is rarely used in production code for sorting data. However, the concept is fundamental in education. Some specialized situations use bubble sort-like approaches, but modern systems use more efficient algorithms.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1780333368315\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">4. <strong>Can I sort in descending order with bubble sort?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, just change the comparison from > to &lt;. Instead of swapping when the left element s larger, swap when it is smaller. This sorts the array in descending order.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1780333377955\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">5. <strong>How does bubble sort compare to other algorithms?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Bubble sort is slower than quicksort, mergesort, and heapsort. However, it uses less space than mergesort and is simpler to understand. For production code, use optimized sorting provided by your language libraries.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Imagine you have a pile of unsorted cards on a table. You want to arrange them in order. One simple approach is to compare adjacent cards and swap them if they are in the wrong order. You repeat this process until all cards are sorted. This is exactly how bubble sort works. Bubble sort is [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":115159,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"views":"45","authorinfo":{"name":"Vishalini Devarajan","url":"https:\/\/www.guvi.in\/blog\/author\/vishalini\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/06\/bubble-sort-in-c-300x115.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/113720"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=113720"}],"version-history":[{"count":3,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/113720\/revisions"}],"predecessor-version":[{"id":115160,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/113720\/revisions\/115160"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/115159"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=113720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=113720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=113720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}