From 86baeb16e2117e553353ccf1231fb7648f51c10c Mon Sep 17 00:00:00 2001 From: Maksim Date: Wed, 2 Oct 2019 21:06:30 +1000 Subject: [PATCH] - --- .../sorting_array/RandomIntArray.class | Bin 0 -> 2832 bytes .../sorting_array/SortingArrayApp.class | Bin 0 -> 1945 bytes sorting_array/sorting_array.iml | 11 ++ sorting_array/src/SortingArrayApp.java | 177 ++++++++++++++++++ 4 files changed, 188 insertions(+) create mode 100644 sorting_array/out/production/sorting_array/RandomIntArray.class create mode 100644 sorting_array/out/production/sorting_array/SortingArrayApp.class create mode 100644 sorting_array/sorting_array.iml create mode 100644 sorting_array/src/SortingArrayApp.java diff --git a/sorting_array/out/production/sorting_array/RandomIntArray.class b/sorting_array/out/production/sorting_array/RandomIntArray.class new file mode 100644 index 0000000000000000000000000000000000000000..2f550f0b282cfe94ace1585bb2c37833afc97b8c GIT binary patch literal 2832 zcmaJ?-)|IE7(I7)w!7U9g|;leW(&0_mO@)WehB>m6hW#jMG6$;=WKUqr)+nd=@vl~ zeX#mqf>9F_6I5U&5)u?4(i$;5=o|lli4Xc@G(Px@1i|y&ncWsJrs>R`d++ys=bZ1{ zySMLucMHH8o;YS< zyFB*BuutCo^4K540on0N3Jl}BPS)}4yyI`st1RrYu%|;L@vwc;ZW+(JnHGN+!)Nv7?ST_L_NXpI zT@>P@o*U#>NHq9S89O`J(wFDTV5`87WbBlaDJ7k`9EHUsZg$M^@?Cj{{38urnsda> zrkzt%<)qEfc-qN0dB@)pA9HMPV95Uvi3CV$JVgoma$cS>4N_JOms0!F1`CXm#u#%A zGgRvw zcQa|nGck-4q{)55$e0)bX{f^zzkyPS#V+j=Z0K4@Lpt^Ais?oZEP-Eja2u&e;jjJI%!DlsZ5MZ0! zC@9AcRG|y?oSDaUSzU@J`IgaBtUw=#hl2cP870w8H(?x@LZqkB`aXUGW#NumtAK^9 zRaU!l645#F(Qgs!od~N-5B`wSVO@F=$~f*uj91re^s?kGChx^c0v8C4OkPELPqT1X z%9Ow~iJJ)`Mo=RZ*-^cPWcvo~8ik4~&;ud#|H>#U$cH6ofr1 zZ*_#%8407dfaNO_M)SG|mv3Sg8C51ClbBsSXA)JDsAj-~K?*-fc#WTC5_6{s)e{hl zAc}qx+D|eE=;r-Y_7Eu^#vUBe#OerV86gU>h>j?#ycf-AA!wm93!(d16vZ=2-N6b@ zuEOeo&vk~0@a@)FR9Mo}3or#$f~c;^w~=NKE@Hm!fqoe2W_IAgr3Zh?r zTQbg<#4jD~d=&pY!Ed!EeF_C1zkHP?jbp?gqWr(fr@tZm7>N;*$dW{kBs`KBBMFa{ z$FT(`OXAPxArTT{@y-&7&VYo7A3#@xE=F62^?~$n5V%0E3s0j=Xn@Vuc|?*|G{)aU zSGdQw`8?g@(**V!E&e*CIL-2JmcWE)<|3H75|}!zqwj<`!uJqTiM!0C@EkS-ST559 zf~C!9(PQ!QpG9E*ARc6P!#j_vJxLnesxZDM+d z;!RMzx6q1rOL$ElxQ$x85RnLSB^UzaB3>0Dg7+TERXs8Luv<&8uLa%T8o(}~RX|$J z?bc^ls)sadGH|ajE3>qF72W%*{!I32^gm9c_s`xLqJNL|-)HMTAo>r9{v%>L!#00h zVo=57)D|$IL)`;dfZZF}X_k9V%dn zO!GoX7El=s+Y?5IzC(VIiCTGB&3<@5>FPA4)hrC0C8bYD=~L9;9BG~By>@}-DA1DU z2~))dze-U8l?(j>V-H=#q5t>NxX9EIuoJtf-xGicZNjk}M8Qfe{# zc87^Vyu*qYPmT%Y;_;i(+@T4`Bk8k9*6I^E^QYwsZS@tgU*(1OwZl4!nS__*2f6k literal 0 HcmV?d00001 diff --git a/sorting_array/out/production/sorting_array/SortingArrayApp.class b/sorting_array/out/production/sorting_array/SortingArrayApp.class new file mode 100644 index 0000000000000000000000000000000000000000..bbb0e7a739a79e17c51abcc6957303aabed403ba GIT binary patch literal 1945 zcmaJ?OH&+G6#nkKx@np~h5(WvX~f46UP-__223O*(ZPYlfEbea=$UDtp{IN7>A~P* zWmOg_%U#N)?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U2V*0>#sB~S literal 0 HcmV?d00001 diff --git a/sorting_array/sorting_array.iml b/sorting_array/sorting_array.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/sorting_array/sorting_array.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/sorting_array/src/SortingArrayApp.java b/sorting_array/src/SortingArrayApp.java new file mode 100644 index 0000000..2b9e76b --- /dev/null +++ b/sorting_array/src/SortingArrayApp.java @@ -0,0 +1,177 @@ +import java.time.Clock; +import java.util.Random; + +import static java.lang.Math.abs; + +public class SortingArrayApp { + public static void main(String[] args) { + RandomIntArray arr1, arr2, arr3, arr4; + + Clock clock = Clock.systemDefaultZone(); + long startTime, endTime; + + arr1 = new RandomIntArray(10); + arr2 = new RandomIntArray(100000); + arr3 = new RandomIntArray(100000); + arr4 = new RandomIntArray(100000); + + // Random array test + arr1.printArray("random arr1"); + + // Insert item in array test + arr1.insertItem(2, 0); + arr1.printArray("arr1: insert item"); + + // Delete item from array test + arr1.deleteItem(2); + arr1.printArray("arr1: delete item"); + + // Search item in array test + int index = arr1.searchItem(4); + if (index != -1) { + System.out.println("index item is " + index); + } + else { + System.out.println("no value"); + } + + System.out.println(); + + // Bubble sort test + startTime = clock.millis(); + arr2.bubbleSort(); + endTime = clock.millis(); + + System.out.println("arr2: bubble sort: sorted for " + (endTime - startTime) + " ms"); + + // Insert sort test + startTime = clock.millis(); + arr3.insertSort(); + endTime = clock.millis(); + + System.out.println("arr3: insert sort: sorted for " + (endTime - startTime) + " ms"); + + // Selection sort test + startTime = clock.millis(); + arr4.selectionSort(); + endTime = clock.millis(); + + System.out.println("arr4: selection sort: sorted for " + (endTime - startTime) + " ms"); + } + + +} + +class RandomIntArray { + private int[] arr; + private int num; + + RandomIntArray(int _num) { + num = _num; + + generateRandomArray(num); + } + + private void generateRandomArray(int num) { + Random random = new Random(); + + arr = new int[num]; + for (int i = 0; i < num; i++) { + arr[i] = abs(random.nextInt() % 10); + } + } + + public void printArray(String label) { + System.out.println(label); + + System.out.print("[ "); + for (int i : arr) { + System.out.print(i + " "); + } + + System.out.println("]"); + System.out.println(); + } + + public void insertItem(int index, int val) { + if (index < 0 || index >= num) { + System.out.println("Error: Wrong index"); + return; + } + + for (int i = num-1; i > index; i--) { + arr[i] = arr[i-1]; + } + arr[index] = val; + } + + public void deleteItem(int index) { + if (index < 0 || index >= num) { + System.out.println("Error: wrong index"); + return; + } + + for (int i = index; i < num-1; i++) { + arr[i] = arr[i+1]; + } + arr[num-1] = 0; + } + + public int searchItem(int val) { + for (int i = 0; i < num-1; i++) { + if (arr[i] == val) { + return i; + } + } + + return -1; + } + + public void bubbleSort() { + for (int i = 1; i <= num; i++) { + for (int j = 0; j < num-i; j++) { + if (arr[j] > arr[j+1]) { + changeItems(j, j+1); + } + } + } + } + + public void insertSort() { + for (int i = 1; i < num; i++) { + int temp = arr[i]; + + while(i > 0 && arr[i-1] > temp) { + arr[i] = arr[i-1]; + --i; + } + + arr[i] = temp; + } + + } + + public void selectionSort() { + for (int i = 0; i < num; i++) { + int min = arr[i]; + int idx = i; + + for (int j = i+1; j < num; j++) { + if (arr[j] < min) { + min = arr[j]; + idx = j; + } + } + + arr[idx] = arr[i]; + arr[i] = min; + } + } + + private void changeItems(int idx1, int idx2) { + int tmp = arr[idx1]; + + arr[idx1] = arr[idx2]; + arr[idx2] = tmp; + } +}