-
Notifications
You must be signed in to change notification settings - Fork 0
/
65111674.fd53611c.js
1 lines (1 loc) · 4.42 KB
/
65111674.fd53611c.js
1
(window.webpackJsonp=window.webpackJsonp||[]).push([[33],{107:function(e,t,a){"use strict";a.r(t),a.d(t,"frontMatter",(function(){return s})),a.d(t,"metadata",(function(){return d})),a.d(t,"toc",(function(){return c})),a.d(t,"BubbleSort",(function(){return u})),a.d(t,"default",(function(){return m}));var r=a(147),n=a(149),i=a(0),l=a.n(i),o=a(148),b=a(200),s={id:"BubbleSort",title:"Bubble Sort",sidebar_label:"Bubble Sort"},d={unversionedId:"Algorithms/Sorting/BubbleSort",id:"Algorithms/Sorting/BubbleSort",isDocsHomePage:!1,title:"Bubble Sort",description:"Idea",source:"@site/docs/Algorithms/Sorting/BubbleSort.md",slug:"/Algorithms/Sorting/BubbleSort",permalink:"/docs/Algorithms/Sorting/BubbleSort",version:"current",sidebar_label:"Bubble Sort",sidebar:"Algorithms",next:{title:"Odd-Even Sort",permalink:"/docs/Algorithms/Sorting/OddEvenSort"}},c=[{value:"Idea",id:"idea",children:[]},{value:"Visualization",id:"visualization",children:[]},{value:"Implementation",id:"implementation",children:[]},{value:"Time and Space Analysis",id:"time-and-space-analysis",children:[]},{value:"Properties",id:"properties",children:[]},{value:"Variants",id:"variants",children:[]}],u=function(e){var t=e.data,a=l.a.useState(0),r=a[0],n=a[1];return Object(o.b)("div",{className:"controller"},Object(o.b)(b.a,{data:t[r],mdxType:"Array"}),Object(o.b)(b.d,{data:t[r],mdxType:"VerticalBarChart"}),Object(o.b)("br",null),Object(o.b)(b.b,{onClick:function(){return n(r-1)},disabled:0==r,mdxType:"Button"},"Previous"),Object(o.b)(b.b,{onClick:function(){return n(r+1)},disabled:r==t.length-1,mdxType:"Button"},"Next"))},p={toc:c,BubbleSort:u};function m(e){var t=e.components,a=Object(n.a)(e,["components"]);return Object(o.b)("wrapper",Object(r.a)({},p,a,{components:t,mdxType:"MDXLayout"}),Object(o.b)("h2",{id:"idea"},"Idea"),Object(o.b)("ul",null,Object(o.b)("li",{parentName:"ul"},"compare each item wth the item next to it, and swap positions if required"),Object(o.b)("li",{parentName:"ul"},"the largest element will be bubbled to the end of the array"),Object(o.b)("li",{parentName:"ul"},"algorithm repeats until we have a pass without swapping any elements")),Object(o.b)("h2",{id:"visualization"},"Visualization"),Object(o.b)(u,{data:[[5,1,4,2,8],[1,5,4,2,8],[1,4,5,2,8],[1,4,2,5,8],[1,2,4,5,8]],mdxType:"BubbleSort"}),Object(o.b)("h2",{id:"implementation"},"Implementation"),Object(o.b)("ul",null,Object(o.b)("li",{parentName:"ul"},"for every step, move largest element in left unsorted array to the end; in this caes, avoid inner loop to iterate through right sorted subarray"),Object(o.b)("li",{parentName:"ul"},"for every step, swap unordered adjacent pair until no swap is needed "),Object(o.b)("li",{parentName:"ul"},"note that in one pass, more than one element can be placed into their final positions, so the position after last swap is sorted and do not need to traverse again")),Object(o.b)("pre",null,Object(o.b)("code",Object(r.a)({parentName:"pre"},{className:"language-java"}),"void bubbleSort(int arr[]) { \n int n = arr.length; \n for (int i = 0; i < n-1; i++) \n for (int j = 0; j < n-i-1; j++) \n if (arr[j] > arr[j+1]) { \n // swap arr[j+1] and arr[i] \n int temp = arr[j]; \n arr[j] = arr[j+1]; \n arr[j+1] = temp; \n } \n} \n")),Object(o.b)("h2",{id:"time-and-space-analysis"},"Time and Space Analysis"),Object(o.b)("ul",null,Object(o.b)("li",{parentName:"ul"},"Time O(n^2)"),Object(o.b)("li",{parentName:"ul"},"Space O(1)")),Object(o.b)("h2",{id:"properties"},"Properties"),Object(o.b)("ul",null,Object(o.b)("li",{parentName:"ul"},"the algorithm garantees that the list is sorted"),Object(o.b)("li",{parentName:"ul"},"Turtle and Hare Problem:",Object(o.b)("ul",{parentName:"li"},Object(o.b)("li",{parentName:"ul"},"larger elements at the beginning get swapped to the end quickly, but small elements at the end get swapped very slowly to the beginning"),Object(o.b)("li",{parentName:"ul"},"this property is good to find k largest elements - use outer loop of bubble sort for k times")))),Object(o.b)("h2",{id:"variants"},"Variants"),Object(o.b)("ul",null,Object(o.b)("li",{parentName:"ul"},Object(o.b)("a",Object(r.a)({parentName:"li"},{href:"https://www.geeksforgeeks.org/odd-even-sort-brick-sort/"}),"Odd Even Sort / Brick Sort")),Object(o.b)("li",{parentName:"ul"},Object(o.b)("a",Object(r.a)({parentName:"li"},{href:"https://www.geeksforgeeks.org/cocktail-sort/"}),"Cocktail Sort"))))}m.isMDXComponent=!0}}]);