{"id":87,"date":"2020-12-08T14:46:33","date_gmt":"2020-12-08T14:46:33","guid":{"rendered":"https:\/\/manishshrivastava.com\/blog\/?p=87"},"modified":"2020-12-08T14:52:37","modified_gmt":"2020-12-08T14:52:37","slug":"bubble-sort-in-ruby-code-simple-explained","status":"publish","type":"post","link":"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/","title":{"rendered":"Simple Bubble Sort Algorithm in Ruby &#8211; Explained"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"750\" height=\"452\" src=\"https:\/\/manishshrivastava.com\/blog\/wp-content\/uploads\/2020\/12\/Screenshot-from-2020-12-08-19-57-23.png\" alt=\"Bubble\" class=\"wp-image-101\" srcset=\"https:\/\/manishshrivastava.com\/blog\/wp-content\/uploads\/2020\/12\/Screenshot-from-2020-12-08-19-57-23.png 750w, https:\/\/manishshrivastava.com\/blog\/wp-content\/uploads\/2020\/12\/Screenshot-from-2020-12-08-19-57-23-300x181.png 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Can you write a Bubble sort in ruby?<\/h2>\n\n\n\n<p>The interviewer usually asks <a href=\"https:\/\/en.wikipedia.org\/wiki\/Sorting_algorithm\">sorting algorithm<\/a> and mostly the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bubble_sort\">bubble sorting methods<\/a> in the their interview. As that is one of the most commonly asked questions in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Sorting_algorithm\">sorting algorithm<\/a>. So, I thought to share that in <a href=\"https:\/\/manishshrivastava.com\/blog\/\">my blog here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5133\">Bubble Sort<\/h2>\n\n\n\n<p id=\"11b3\">The bubble sort makes the larger elements (\u201cbig bubble\u201d) towards the end and the smaller elements (\u201csmall bubble\u201d) towards the starting point until all the elements reach in their correct location. i.e in proper sorted order from Small to Big sort.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"># A method to define the bubble sort\ndef bubble_sort(array)\n  # condition: When array has more than one element\n  if array.count > 1\n    swap = true\n    # Loop: Run until swap is true\n    while swap\n      swap = false\n      # Loop: Run loop with array counts and swap if \n      (array.length-1).times do |z|\n        if array[z] > array[z+1]\n          # swap if current number element is greater than next number element.\n          array[z], array[z+1] =  array[z+1], array[z]\n          # as we we have current number element is greater than next number element\n          # so we updated more swap needed.\n          swap = true\n        end\n      end\n    end\n    # While loop will stop once  array[z] > array[z+1] will be false \n    # then swap will become false line number 8.\n  end\n  array\nend\n\n\nprint \"Befor sort - #{[7,9,3,5,4,2,1]}\"\nprint \"\\n\"\nprint \"After sort - #{bubble_sort([7,9,3,5,4,2,1])}\"\nprint \"\\n\"\n\n\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>When you will run the above code( <a href=\"https:\/\/gist.github.com\/manish-shrivastava\/c857083e23cf14de0d767d4545f677f2\">Public Gist here<\/a>). tath will Result as below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"512\" height=\"103\" src=\"https:\/\/manishshrivastava.com\/blog\/wp-content\/uploads\/2020\/12\/Screenshot-from-2020-12-08-18-45-19.png\" alt=\"bubble sorted result\" class=\"wp-image-93\" srcset=\"https:\/\/manishshrivastava.com\/blog\/wp-content\/uploads\/2020\/12\/Screenshot-from-2020-12-08-18-45-19.png 512w, https:\/\/manishshrivastava.com\/blog\/wp-content\/uploads\/2020\/12\/Screenshot-from-2020-12-08-18-45-19-300x60.png 300w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/figure>\n\n\n\n<p>Thank you!<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Can you write a Bubble sort in ruby? The interviewer usually asks sorting algorithm and mostly the bubble sorting methods in the their interview. As that is one of the most commonly asked questions in sorting algorithm. So, I thought to share that in my blog here. Bubble Sort The bubble sort makes the larger &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Simple Bubble Sort Algorithm in Ruby &#8211; Explained&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[24,28,26,30,25],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Simple Bubble Sort Algorithm in Ruby - Explained - Manish Blog<\/title>\n<meta name=\"description\" content=\"Simple Bubble Sort Algorithm in Ruby - Explained\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Simple Bubble Sort Algorithm in Ruby - Explained - Manish Blog\" \/>\n<meta property=\"og:description\" content=\"Simple Bubble Sort Algorithm in Ruby - Explained\" \/>\n<meta property=\"og:url\" content=\"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/\" \/>\n<meta property=\"og:site_name\" content=\"Manish Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-08T14:46:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-08T14:52:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/manishshrivastava.com\/blog\/wp-content\/uploads\/2020\/12\/Screenshot-from-2020-12-08-19-57-23.png\" \/>\n<meta name=\"author\" content=\"manishshrivastava\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"manishshrivastava\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/\"},\"author\":{\"name\":\"manishshrivastava\",\"@id\":\"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/e4d02d0fe4a05267401de130fac850f0\"},\"headline\":\"Simple Bubble Sort Algorithm in Ruby &#8211; Explained\",\"datePublished\":\"2020-12-08T14:46:33+00:00\",\"dateModified\":\"2020-12-08T14:52:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/\"},\"wordCount\":116,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/19d0df1c38b86606412b5c821e67e67e\"},\"keywords\":[\"bubble sort\",\"learn ruby\",\"programming in ruby\",\"sorting in ruby\",\"sorting method\"],\"articleSection\":[\"Algorithm\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/\",\"url\":\"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/\",\"name\":\"Simple Bubble Sort Algorithm in Ruby - Explained - Manish Blog\",\"isPartOf\":{\"@id\":\"https:\/\/manishshrivastava.com\/blog\/#website\"},\"datePublished\":\"2020-12-08T14:46:33+00:00\",\"dateModified\":\"2020-12-08T14:52:37+00:00\",\"description\":\"Simple Bubble Sort Algorithm in Ruby - Explained\",\"breadcrumb\":{\"@id\":\"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/manishshrivastava.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Algorithm\",\"item\":\"https:\/\/manishshrivastava.com\/blog\/category\/algorithm\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Simple Bubble Sort Algorithm in Ruby &#8211; Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/manishshrivastava.com\/blog\/#website\",\"url\":\"https:\/\/manishshrivastava.com\/blog\/\",\"name\":\"Manish Blog\",\"description\":\"Rubyist, Realist, Pragmatist, and Believer\",\"publisher\":{\"@id\":\"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/19d0df1c38b86606412b5c821e67e67e\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/manishshrivastava.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/19d0df1c38b86606412b5c821e67e67e\",\"name\":\"manish shrivastava\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8d35275f95d1cd3202c89c1693336453?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8d35275f95d1cd3202c89c1693336453?s=96&d=mm&r=g\",\"caption\":\"manish shrivastava\"},\"logo\":{\"@id\":\"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/99interview.com\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/e4d02d0fe4a05267401de130fac850f0\",\"name\":\"manishshrivastava\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ba8ddd15351cbf6b58b3d8e16ce94b3b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ba8ddd15351cbf6b58b3d8e16ce94b3b?s=96&d=mm&r=g\",\"caption\":\"manishshrivastava\"},\"description\":\"Holds 15+ years of experience as a Software Development Expert. His vision is to share knowledge related to How Software can add value to anyone's life. Manish is a Software Developer and Open source contributor. He is an experienced developer with 15+ years of experience in creating awesome web and app projects based on specific requirements using Ruby, React, and sometimes python. He says, He is a Rubyist, Realist, Pragmatist, and Believer.\",\"sameAs\":[\"https:\/\/manishshrivastava.com\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Simple Bubble Sort Algorithm in Ruby - Explained - Manish Blog","description":"Simple Bubble Sort Algorithm in Ruby - Explained","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/","og_locale":"en_US","og_type":"article","og_title":"Simple Bubble Sort Algorithm in Ruby - Explained - Manish Blog","og_description":"Simple Bubble Sort Algorithm in Ruby - Explained","og_url":"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/","og_site_name":"Manish Blog","article_published_time":"2020-12-08T14:46:33+00:00","article_modified_time":"2020-12-08T14:52:37+00:00","og_image":[{"url":"https:\/\/manishshrivastava.com\/blog\/wp-content\/uploads\/2020\/12\/Screenshot-from-2020-12-08-19-57-23.png"}],"author":"manishshrivastava","twitter_card":"summary_large_image","twitter_misc":{"Written by":"manishshrivastava","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/#article","isPartOf":{"@id":"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/"},"author":{"name":"manishshrivastava","@id":"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/e4d02d0fe4a05267401de130fac850f0"},"headline":"Simple Bubble Sort Algorithm in Ruby &#8211; Explained","datePublished":"2020-12-08T14:46:33+00:00","dateModified":"2020-12-08T14:52:37+00:00","mainEntityOfPage":{"@id":"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/"},"wordCount":116,"commentCount":0,"publisher":{"@id":"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/19d0df1c38b86606412b5c821e67e67e"},"keywords":["bubble sort","learn ruby","programming in ruby","sorting in ruby","sorting method"],"articleSection":["Algorithm"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/","url":"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/","name":"Simple Bubble Sort Algorithm in Ruby - Explained - Manish Blog","isPartOf":{"@id":"https:\/\/manishshrivastava.com\/blog\/#website"},"datePublished":"2020-12-08T14:46:33+00:00","dateModified":"2020-12-08T14:52:37+00:00","description":"Simple Bubble Sort Algorithm in Ruby - Explained","breadcrumb":{"@id":"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/manishshrivastava.com\/blog\/bubble-sort-in-ruby-code-simple-explained\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/manishshrivastava.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Algorithm","item":"https:\/\/manishshrivastava.com\/blog\/category\/algorithm\/"},{"@type":"ListItem","position":3,"name":"Simple Bubble Sort Algorithm in Ruby &#8211; Explained"}]},{"@type":"WebSite","@id":"https:\/\/manishshrivastava.com\/blog\/#website","url":"https:\/\/manishshrivastava.com\/blog\/","name":"Manish Blog","description":"Rubyist, Realist, Pragmatist, and Believer","publisher":{"@id":"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/19d0df1c38b86606412b5c821e67e67e"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/manishshrivastava.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/19d0df1c38b86606412b5c821e67e67e","name":"manish shrivastava","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8d35275f95d1cd3202c89c1693336453?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8d35275f95d1cd3202c89c1693336453?s=96&d=mm&r=g","caption":"manish shrivastava"},"logo":{"@id":"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/99interview.com"]},{"@type":"Person","@id":"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/e4d02d0fe4a05267401de130fac850f0","name":"manishshrivastava","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/manishshrivastava.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ba8ddd15351cbf6b58b3d8e16ce94b3b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ba8ddd15351cbf6b58b3d8e16ce94b3b?s=96&d=mm&r=g","caption":"manishshrivastava"},"description":"Holds 15+ years of experience as a Software Development Expert. His vision is to share knowledge related to How Software can add value to anyone's life. Manish is a Software Developer and Open source contributor. He is an experienced developer with 15+ years of experience in creating awesome web and app projects based on specific requirements using Ruby, React, and sometimes python. He says, He is a Rubyist, Realist, Pragmatist, and Believer.","sameAs":["https:\/\/manishshrivastava.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/manishshrivastava.com\/blog\/wp-json\/wp\/v2\/posts\/87"}],"collection":[{"href":"https:\/\/manishshrivastava.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/manishshrivastava.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/manishshrivastava.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/manishshrivastava.com\/blog\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":13,"href":"https:\/\/manishshrivastava.com\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":106,"href":"https:\/\/manishshrivastava.com\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions\/106"}],"wp:attachment":[{"href":"https:\/\/manishshrivastava.com\/blog\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/manishshrivastava.com\/blog\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/manishshrivastava.com\/blog\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}