From dd580b936407cc17b3022918e40ad949efb274da Mon Sep 17 00:00:00 2001 From: extrathings Date: Sat, 3 Oct 2020 16:34:37 +0530 Subject: [PATCH 1/2] Insertion Sort Added --- Insertion.java | 38 ++++++++++++++++++++++++++++++++++++++ InsertionSort.class | Bin 0 -> 1252 bytes 2 files changed, 38 insertions(+) create mode 100644 Insertion.java create mode 100644 InsertionSort.class diff --git a/Insertion.java b/Insertion.java new file mode 100644 index 0000000..d2a5b27 --- /dev/null +++ b/Insertion.java @@ -0,0 +1,38 @@ +class InsertionSort { + /*Function to sort array using insertion sort*/ + void sort(int arr[]) + { + int n = arr.length; + for (int i = 1; i < n; ++i) { + int key = arr[i]; + int j = i - 1; + + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } + } + + // For print array + static void printArray(int arr[]) + { + int n = arr.length; + for (int i = 0; i < n; ++i) + System.out.print(arr[i] + " "); + + System.out.println(); + } + + // Driver method + public static void main(String args[]) + { + int arr[] = { 12, 11, 13, 5, 6 }; + + InsertionSort ob = new InsertionSort(); + ob.sort(arr); + + printArray(arr); + } +} \ No newline at end of file diff --git a/InsertionSort.class b/InsertionSort.class new file mode 100644 index 0000000000000000000000000000000000000000..2fe7d7bdb4c73feb57ba9e519c3c82d3c1bb3880 GIT binary patch literal 1252 zcmaJ>TTc@~6#k~W?UrSM3bfo>Zi4m#tvB#eAki8k30RX7Ov8&qyMcvvx7i&?c;`15 z6W@pl&-wrc4R5~qGko+H7!$-ZZ6gqf4?A<_%sJn8&Nnmr{m=0UfX8^E!v{r!YQT>G zLuixl@Jxl<>zVx8rdbjUff>uT#2kYXPpq^ch!zdHfmRro!V9~eFsl^hY*SKru?}Q8 znOCl5i-K@XUd?tAXS;z8gcy3Ne9O!^c8QDEme?T66WkUa<;E8hjr4jQlxx}RSsk6| z($H-nj0i*5HTVqvnq-p@R{;_$5~9~YpKMgCM^w$WzzlyA+jk$|gg{qGg*ex2tQWyUf=rW$0T<4bc2Nbuz2Y z#iIL|NyXCBYD_Ip`@{bC7)pix>9G0%5i-;_a+N28b`Q>e)LmgUzo)Q+vu~2gS7%B> zC)*E&uyts3xHK0tTkgj#=8vA%t5L8G?EWKVzq8XKeTv+9;lGYO_X8I0{9L zCr@US+33JNdOkr*M$^X>h(p}i^AY_|@*Se*yommKV_`~-5OoiFX&$6kG)AgPQu>9| zNq^H$3vMIcq+20LBwafARWB+bBVSZQt@)xq)Rr#>g88Br3gj2Rpuc#8=+Z>$5V6!} ujGW6Hp~j`eQH&7RC=xVIk}L{YQlKPKGzW=og1&orKr5ZBDLkY Date: Sat, 3 Oct 2020 16:39:36 +0530 Subject: [PATCH 2/2] Bubble Sort Added --- BubbleSort.class | Bin 0 -> 1319 bytes BubbleSort.java | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 BubbleSort.class create mode 100644 BubbleSort.java diff --git a/BubbleSort.class b/BubbleSort.class new file mode 100644 index 0000000000000000000000000000000000000000..51b5984e0bdc0c8fc20509ff21081e5a3b948c2f GIT binary patch literal 1319 zcmaJ>ZBNrs6n^g7u47iI=!PIeK+uip=IeZ!Eb0sq1#2>cnSSwZyMfBqE^TMRcYlQ6 zOo)k}^#dkq_yKc?-Y|RFa56(V-g95N9x7 z@>jgr=FUpdYP=N9fFXX%cI=?Spk#B)14v*%N7BF{7<wX|QWaakADYX~|vfbii z&vt@Z;0fL-4-w`O1A{ObhC6&sR9&aZgQs?|N|GONC-BKPyO4|ALmuR7J1gZRhHy;B zaRVu&8IBzgpF!(NG#O%lfZVbSal*ieOjHjo+m4Qt49UB_Mx!lit`{UR3PmpC22Npu z?i&B@D1P?<)*MDF#DYi`E49Jiaa?Ps_+@>3YV%+0Zq_xFZc1)T-MO41=Jvd%n75_#;12VGtO&K_uImPV;PLK~ z2v*&eKN!cwBrf5yjw`bER~e>vx@|xY>z4Nu-F33Z@yU8u9ALgX)oAX@yGNiOqRQLtnjMI80@oN~jJjhiIF9hDZSMQR z*Kw0!W?yaxQPy#bVf_E5H2P6~n4qS{Xm$aWUiwhLkR>TYJCi4~pTItlz>uR=UP~Er zf@qhfZ$mfWVSZ~)sbt2~)&}kCPLX7$Gkvlf82$)7pDBE%VA#abkiZW*+r;n=jZuOCj+1kW2&Hj?_Hhhj znj{HJ|C$hXoAd$XiC$EjE`cd@fA0%M>z^>TXzE{+b;Z1G)m3xKs%z$mRgas7RoBh5 zRZp0QthyODpIJ-!bDPKzjiJd+oR-SWP8T9J87jp%E>b5-lwJjssA7uH&J$h?1r%vd U5RVJ=mT)~}$>_&e#vDfe0=qgTTmS$7 literal 0 HcmV?d00001 diff --git a/BubbleSort.java b/BubbleSort.java new file mode 100644 index 0000000..42404b1 --- /dev/null +++ b/BubbleSort.java @@ -0,0 +1,35 @@ +class BubbleSort +{ + void bubbleSort(int arr[]) + { + int n = arr.length; + for (int i = 0; i < n-1; i++) + for (int j = 0; j < n-i-1; j++) + if (arr[j] > arr[j+1]) + { + // swap temp and arr[i] + int temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + } + } + + /* Prints the array */ + void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i