{"id":1405,"date":"2016-08-04T07:50:49","date_gmt":"2016-08-04T07:50:49","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1405"},"modified":"2017-12-17T17:18:34","modified_gmt":"2017-12-17T17:18:34","slug":"sorting","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/sorting\/","title":{"rendered":"Sorting"},"content":{"rendered":"<p><strong>Bubble Sort<\/strong><\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/F13_wsHDIG4\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic void bubbleSort()\r\n{\r\n\tfor (int i = arrNumbers.length-1; i&gt;1 ; i--) \r\n\t{\t\r\n\t\tfor (int j = 0; j &lt; i; j++) \r\n\t\t{\t\t\t\r\n\t\t\tif(arrNumbers&#x5B;j] &gt; arrNumbers&#x5B;j+1])\r\n\t\t\t{\r\n\t\t\t\tswapValuesAtIndex(j, j+1);\t\t\t\t\t\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/*IterationDisplay(arrNumbers, j);*\/\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Selection Sort<\/strong><br \/>\nSelection sort works by dividing the list into 2 Parts. Sorted and Unsorted.Taking one element at a time as Minimum element it works by comparing the minimum element with other elements in the list.<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/f8hXR_Hvybo\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic void selectionSort()\r\n{\r\n\tint minElement = 0;\r\n\t\r\n\tfor (int i = 0; i&lt; arrNumbers.length ; i++) \r\n\t{\r\n     \tminElement = i;\r\n\t\t\r\n\t\tfor (int j = i; j &lt; arrNumbers.length; j++) \r\n\t\t{\t\t\t\r\n\t\t\tif(arrNumbers&#x5B;minElement] &gt; arrNumbers&#x5B;j])\r\n\t\t\t{\t\t\t\t\t\t\r\n\t\t\t\tminElement =  j;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tswapValuesAtIndex(minElement, i);\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Insertion Sort<\/strong><br \/>\nInsertion  sort is the best sorting method compared to others.The list is divided into sorted and unsorted portion. Once a no is selected for comparison it will not ened without placing the no at the correct location.<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/DFG-XuyPYUQ\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic void insertionSort()\r\n{\t\r\n\tfor (int i = 1; i &lt; arrNumbers.length; i++) \r\n\t{\r\n\t\tint j = i;\r\n\t\tint toCompare = arrNumbers&#x5B;i];\r\n\t\t\r\n\t\t\/\/holds no to Insert - arrNumbers&#x5B;j-1]\r\n\t\twhile((j&gt;0) &amp;&amp; (arrNumbers&#x5B;j-1] &gt; toCompare))\r\n\t\t{\r\n\t\t\tarrNumbers&#x5B;j] = arrNumbers&#x5B;j-1];\r\n\t\t\tj--;\r\n\t\t}\r\n\t\t\r\n\t\tarrNumbers&#x5B;j] =  toCompare;\t\t\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Bubble Sort public void bubbleSort() { for (int i = arrNumbers.length-1; i&gt;1 ; i&#8211;) { for (int j = 0; j &lt; i; j++) { if(arrNumbers&#x5B;j] &gt; arrNumbers&#x5B;j+1]) { swapValuesAtIndex(j, j+1); } \/*IterationDisplay(arrNumbers, j);*\/ } } } Selection Sort Selection sort works by dividing the list into 2 Parts. Sorted and Unsorted.Taking one element at&hellip; <a href=\"https:\/\/codethataint.com\/blog\/sorting\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[241],"tags":[],"class_list":["post-1405","post","type-post","status-publish","format-standard","hentry","category-data-structures"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1405"}],"version-history":[{"count":13,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1405\/revisions"}],"predecessor-version":[{"id":1410,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1405\/revisions\/1410"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}