From 78172043365f252ae0ba3324095bef25ff8326be Mon Sep 17 00:00:00 2001 From: Sanduni fernando <59954008+Sandunifernando@users.noreply.github.com> Date: Mon, 11 Oct 2021 17:59:21 +0530 Subject: [PATCH] Create SimpleSortingApp.java --- .../Java_Programs/SimpleSortingApp.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 Program's_Contributed_By_Contributors/Java_Programs/SimpleSortingApp.java diff --git a/Program's_Contributed_By_Contributors/Java_Programs/SimpleSortingApp.java b/Program's_Contributed_By_Contributors/Java_Programs/SimpleSortingApp.java new file mode 100644 index 0000000000..512b03a3c1 --- /dev/null +++ b/Program's_Contributed_By_Contributors/Java_Programs/SimpleSortingApp.java @@ -0,0 +1,112 @@ +package packageDA; + +public class SimpleSortingApp { + + public static void main(String[] args) { + + SimpleSorting k = new SimpleSorting(7); + k.insert(8); + k.insert(7); + k.insert(2); + k.insert(6); + k.display(); + k.bubbleSort(); + k.display(); + + } + +} + +class SimpleSorting { + private int[] a;//ref to array a + private int nElems;//number of data items + //------------------------------------------------------ + public SimpleSorting(int max) { + //constructor + this.a = new int[max]; + this.nElems=0; + } + public void insert(int value){ + //put element into array + if(nElems==a.length) { + System.out.print("Array is full"); + return; + } + this.a[this.nElems]=value; + this.nElems++; + // System.out.print(value); + } + public void display(){ + //display array contents + System.out.println("Array content :"); + for(int i=0;i a[j+1]) { + swap(j, j+1); + } + } + } + System.out.println("Numbers sorted using Bubble sorting"); + } + + +//---------------------Selection sort-------------------------------- + + public void selectionSort() { + + for(int i =0; i=0){ + if(a[j]>key){ + a[j+1] = a[j]; + } else { + break; + } + j--; + } + a[j+1] = key; + } + System.out.println("Numbers are sorted using insertionSort..."); + + + + + }} +