forked from cooervo/Algorithms-DataStructures-BigONotation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms.html
750 lines (621 loc) · 25.3 KB
/
algorithms.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
<link
href='http://fonts.googleapis.com/css?family=Open+Sans:800|Titillium+Web:400,600,700,900'
rel='stylesheet' type='text/css'>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1">
<title>Algorithms</title>
<link rel="icon" href="images/icons/favicon.ico">
</head>
<body>
<!-- ======== ASIDE ======== -->
<aside>
<a href="index.html"><img id="logo"
src="images/logo-algos.svg"
onerror="this.onerror=null; this.src='image.svg'"></a>
<a id="menu-button" href="#">Menu</a>
<nav id="responsive-nav">
<ul>
<li><a href="index.html"><img class="aside-icons"
src="images/icons/cheat-sheets.svg"
onerror="this.onerror=null; this.src='image.svg'">big O cheat sheets</a></li>
<li><a href="intro.html"><img class="aside-icons"
src="images/icons/intro-icon.svg"
onerror="this.onerror=null; this.src='image.svg'">intro</a></li>
<li><a href="big-O-notation.html"><img
class="aside-icons" src="images/icons/big-o-icon.svg"
onerror="this.onerror=null; this.src='image.svg'">big O notation</a></li>
<li><a href="data-structures.html"><img
class="aside-icons"
src="images/icons/data-structures-icon.svg"
onerror="this.onerror=null; this.src='image.svg'"><span
class="nav-descriptions">data structures</span></a></li>
<li><a href="algorithms.html"><img
class="aside-icons" src="images/icons/algo-icon.svg"
onerror="this.onerror=null; this.src='image.svg'"><span
class="responsnav-descriptionsive-hide">algorithms</span></a></li>
<li><a
href="https://github.com/cooervo/algortihms-datastructures"
target="_blank"><img class="aside-icons"
src="images/icons/github.svg"
onerror="this.onerror=null; this.src='image.svg'"><span
class="nav-descriptions">Github</span></a></li>
</ul>
</nav>
<div id="share-div">
<ul id="share-list">
<li><a
href="https://www.facebook.com/sharer/sharer.php?u=http://cooervo.github.io/algortihms-datastructures/index.html"><img
class="share-buttons" src="images/icons/FB-share.svg"
onerror="this.onerror=null; this.src='image.svg'"></a></li>
<li><a
href="https://twitter.com/home?status=Check%20this%20website%20with%20%20cheatsheets%20for%20data%20structures%20and%20algorithms%20http://cooervo.github.io/algortihms-datastructures/index.html"><img
class="share-buttons"
src="images/icons/twitter-share.svg"
onerror="this.onerror=null; this.src='image.svg'"></a></li>
<li><a
href="https://plus.google.com/share?url=http://cooervo.github.io/algortihms-datastructures/index.html"><img
class="share-buttons"
src="images/icons/google-plus-share.svg"
onerror="this.onerror=null; this.src='image.svg'"></a></li>
</ul>
</div>
</aside>
<!-- ======== /ASIDE ======== -->
<!-- ======== MAIN ======== -->
<div id="main">
<article>
<h1 class="titles">SEARCHING AND SORTING</h1>
<p>In this section we are going to review searching and
sorting algorithms.</p>
<h2 class="inner-titles">Why is sorting so important</h2>
<ul>
<li>The first step in organizing data is sorting. Lots
of tasks become easier once a data set of items is sorted</li>
<li>Some algorithms like binary search are built around
a sorted data structure.</li>
<li>In accordance to S. Skiena computers have
historically spent more time sorting than doing anything
else. Sorting remains the most ubiquitous combinatorial
algorithm problem in practice.</li>
</ul>
<h2 class="inner-titles">Considerations:</h2>
<ul>
<li>How to sort: descending order or ascending order?</li>
<li>Sorting based on what? An object name
(alphabetically), by some number defined by its
fields/instance variables. Or maybe compare dates,
birthdays, etc.</li>
<li>What happens with equals keys, for example various
people with the same name: John, then sort them by Last
Name.</li>
<li>Does your sorting algorithm sorts in place or needs
extra memory to hold another copy of the array to be
sorted. This is even more important in embedded systems.</li>
</ul>
<p>Java uses Comparable interface for sorting and returns:
+1 if compared object is greater, -1 if compared object is
less and 0 if compared objects are equal.</p>
<p>Sorting becomes more ubiquitous when we think on all the
things we do daily that are previously sorted for us to
understand and have better access to them:</p>
<ul>
<li>Imagine trying to find a phone number in an
unsorted phone book, or searching for a word in an
unsorted dictionary.</li>
<li>Your MP3 player can sort your lists by artists
name, genre, song name, ratings.</li>
<li>Search engines display results in descending order
of importance</li>
<li>Spreadsheets can be sorted in various ways to work
better with their contents</li>
</ul>
<h2 class="inner-titles">Searching</h2>
<p>There are two types of searching algorithms: Those that
need a previously ordered data structure in order to work
properly, and those that don’t need an ordered list.</p>
<p>Searching is also very important for many computing
applications: searching through a search engine, finding a
bank account balance for some client, searching in a large
data set for a particular value, searching in your
directories for some needed file. Many applications rely on
effective search, if your application is complete but takes
long too perform a search and retrieve data it will be
discarded as useless.</p>
<div class="division"></div>
</article>
<article>
<h1 class="titles">SELECTION SORT</h1>
<p>It is called selection sort because it repeatedly
selects the smallest remaining item:</p>
<ol>
<li>Find the smallest element. Swap it with the first
element.</li>
<li>Find the second smallest element. Swap it with the
second element</li>
<li>Find the third smallest element. Swap it with the
third element</li>
<li>Repeat finding the smallest element and swapping in
the correct position until the list is sorted</li>
</ol>
<p>Implementation in java for selection sort:</p>
<div class="code-div">
<pre>
<code class="Jcode">
public class SelectionSort {
public static void selectionSort(Comparable[] array) {
for (int i = 0; i < array.length; i++) {
int min = i;
for (int j = i + 1; j < array.length; j++) {
if (isLess(array[j], array[min])) {
min = j;
}
}
swap(array, i, min);
}
}
<span class="code-comment">
//returns true if Comparable j is less than min</span>
private static boolean isLess(Comparable j, Comparable min) {
int comparison = j.compareTo(min);
return comparison< 0;
}
private static void swap(Comparable[] array, int i, int j) {
Comparable temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static <E> void printArray(E[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
<span class="code-comment">
// Check if array is sorted</span>
public static boolean isSorted(Comparable[] array) {
for (int i = 1; i < array.length; i++) {
if (isLess(array[i], array[i - 1])) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Integer[] intArray = { 34, 17, 23, 35, 45, 9, 1 };
System.out.println("Unsorted Array: ");
printArray(intArray);
System.out.println("\nIs intArray sorted? "
+ isSorted(intArray));
selectionSort(intArray);
System.out.println("\nSelection sort:");
printArray(intArray);
System.out.println("\nIs intArray sorted? "
+ isSorted(intArray));
String[] stringArray = { "z", "g", "c", "o", "a",
"@", "b", "A", "0", "." };
System.out.println("\n\nUnsorted Array: ");
printArray(stringArray);
System.out.println("\n\nSelection sort:");
selectionSort(stringArray);
printArray(stringArray);
}
}
</code>
</pre>
</div>
<p>Output is:</p>
<div class="code-div">
<pre>
<code class="Jcode">
Unsorted Array:
34 17 23 35 45 9 1
Is intArray sorted? false
Selection sort:
1 9 17 23 34 35 45
Is intArray sorted? true
Unsorted Array:
z g c o a @ b A 0 .
Selection sort:
. 0 @ A a b c g o z
</code>
</pre>
</div>
<div class="division"></div>
</article>
<article>
<h1 class="titles">SHELLSORT</h1>
<p>Shell sort is perfect good for embedded systems as it
doesn’t require a lot of extra memory allocation; it is also
useful for small to medium size arrays. Lets see the
following implementation in java code:</p>
<div class="code-div">
<pre>
<code class="Jcode">
public class ShellSort {
public static void shellSort(Comparable[] array) {
int N = array.length;
int h = 1;
while (h < N/3) h = 3*h + 1;
while (h >= 1){
for (int i=h; i<N ; i++) {
for(int j=i; j>=h && isLess(array[j], array[j-h]); j-=h){
swap(array, j, j-h);
}
}
h = h/3;
}
}
<span class="code-comment">
// returns true if Comparable j is less than min</span>
private static boolean isLess(Comparable j, Comparable min) {
int comparison = j.compareTo(min);
return comparison< 0;
}
private static void swap(Comparable[] array, int i, int j) {
Comparable temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static <E> void printArray(E[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
<span class="code-comment">
// Check if array is sorted</span>
public static boolean isSorted(Comparable[] array) {
for (int i = 1; i < array.length; i++) {
if (isLess(array[i], array[i - 1])) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Integer[] intArray = { 34, 1000, 23, 2, 35, 0, 9, 1 };
System.out.println("Unsorted Array: ");
printArray(intArray);
System.out.println("\nIs intArray sorted? " + isSorted(intArray));
shellSort(intArray);
System.out.println("\nSelection sort:");
printArray(intArray);
System.out.println("\nIs intArray sorted? " + isSorted(intArray));
String[] stringArray = { "z", "Z","999", "g", "c", "o",
"a", "@", "b", "A","0", "." };
System.out.println("\n\nUnsorted Array: ");
printArray(stringArray);
System.out.println("\n\nSelection sort:");
shellSort(stringArray);
printArray(stringArray);
}
}
</code>
</pre>
</div>
<p>Output:</p>
<div class="code-div">
<pre>
<code class="Jcode">
Unsorted Array:
34 1000 23 2 35 0 9 1
Is intArray sorted? false
Selection sort:
0 1 2 9 23 34 35 1000
Is intArray sorted? true
Unsorted Array:
z Z 999 g c o a @ b A 0 .
Selection sort:
. 0 999 @ A Z a b c g o z
</code>
</pre>
</div>
<div class="division"></div>
</article>
<article>
<h1 class="titles">MERGESORT</h1>
<p>Mergesort is also called divide and conquer algorithm,
because it divides the original data into smaller pieces of
data to solve the problem. Merge sort works in the following
way:</p>
<ol>
<li>Divide into 2 collections. Mergesort will take the
middle index in the collection and split it into the left
and right parts based on this middle index.</li>
<li>Resulting collections are again recursively splited
and sorted</li>
<li>Once the sorting of the two collections is
finished, the results are merged</li>
<li>Now Mergesort it picks the item which is smaller
and inserts this item into the new collection.</li>
<li>Then selects the next elements and sorts the
smaller element from both collections</li>
</ol>
<div class="code-div">
<pre>
<code class="Jcode">
public class MergeSort {
public int[] sort(int [] array){
mergeSort(array, 0, array.length-1);
return array;
}
private void mergeSort(int[] array, int first, int last) {
<span class="code-comment">
// We need mid to divide problem into smaller size pieces</span>
int mid = (first + last) / 2;
<span class="code-comment">
//If first < last the array must be recursively sorted</span>
if (first < last) {
mergeSort(array, first, mid);
mergeSort(array, mid + 1, last);
}
<span class="code-comment">
//merge solved pieces to get solution to original problem</span>
int a = 0, f = first, l = mid + 1;
int[] temp = new int[last - first + 1];
while (f <= mid && l <= last) {
temp[a++] = array[f] < array[l] ? array[f++] : array[l++];
}
while (f <= mid) {
temp[a++] = array[f++];
}
while (l <= last) {
temp[a++] = array[l++];
}
a = 0;
while (first <= last) {
array[first++] = temp[a++];
}
}
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static void main(String[] args) {
System.out.println("Original array:");
int[] example = { 100, 30, 55, 62, 2, 42, 20, 9, 394, 1, 0 };
printArray(example);
System.out.println("\nMergesort");
MergeSort merge = new MergeSort();
merge.sort(example);
printArray(example);
}
}
</code>
</pre>
</div>
<p>Output:</p>
<div class="code-div">
<pre>
<code class="Jcode">
Original array:
100 30 55 62 2 42 20 9 394 1 0
Mergesort
0 1 2 9 20 30 42 55 62 100 394
</code>
</pre>
</div>
<div class="division"></div>
</article>
<article>
<h1 class="titles">QUICKSORT</h1>
<p>Quick sort is better than merge sort from a memory usage
comparison. Because quick sort doesn’t require additional
storage to work. It only uses a small auxiliary stack.</p>
<p>Skiena discovered via experimentation that a properly
implemented quicksort is typically 2-3 times faster than
mergesort or heapsort</p>
<p>The pseudo-code for quicksort is:</p>
<ol>
<li>If the array contains only 1 element or 0 elements
then the array is sorted.</li>
<li>If the array contains more than 1 element:</li>
<li>Select randomly an element from the array. This is
the "pivot element".</li>
<li>Split into 2 arrays based on pivot element: smaller
elements than pivot go to the first array, the ones above
the pivot go into the second array</li>
<li>Sort both arrays by recursively applying the
Quicksort algorithm.</li>
<li>Merge the arrays</li>
</ol>
<p>
Quicksort may take quadratic time O(n<sup>2</sup>) to
complete but this is highly improbable to occur. This
happens when the partitions are unbalanced. For example, the
first partition is on the smallest item, the second
partition of the next smallest item, so each time the
program just remove one item for each call, leading to a
large number of partitions of large sub arrays .If the
collection of elements is previously randomized the most
probable performance time for quicksort is Θ (n log n). So
if you’re having problems with performance of quicksort, you
can rearrange randomly elements in the collection this will
increase the probability of quicksort to perform in Θ (n log
n).
</p>
<p>Implementation of quicksort:</p>
<div class="code-div">
<pre>
<code class="Jcode">
import java.util.Random;
public class QuickSort {
public void quicksort(int[] array) {
quicksort(array, 0, array.length - 1);
}
private void quicksort(int[] array, int first, int last) {
if (first < last) {
int pivot = partition(array, first, last);
<span class="code-comment">
// Recursive call</span>
quicksort(array, first, pivot - 1);
quicksort(array, pivot + 1, last);
}
}
private int partition(int[] input, int first, int last) {
<span class="code-comment">
/*
*notice how pivot is randomly selected this makes O(n^2)
*very low probability
*/</span>
int pivot = first + new Random().nextInt(last - first + 1);
swap(input, pivot, last);
for (int i = first; i < last; i++) {
if (input[i] <= input[last]) {
swap(input, i, first);
first++;
}
}
swap(input, first, last);
return first;
}
private void swap(int[] input, int a, int b) {
int temp = input[a];
input[a] = input[b];
input[b] = temp;
}
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static void main(String[] args) {
int[] example = { 90, 6456, 20, 34, 65, -1, 54, -15,
1, -999, 55, 529, 0 };
System.out.println("Original Array");
printArray(example);
System.out.println("\n\nQuickSort");
QuickSort sort = new QuickSort();
sort.quicksort(example);
printArray(example);
}
}
</code>
</pre>
</div>
<p>Output:</p>
<div class="code-div">
<pre>
<code class="Jcode">
Original Array
90 6456 20 34 65 -1 54 -15 1 -999 55 529 0
QuickSort
-999 -15 -1 0 1 20 34 54 55 65 90 529 6456
</code>
</pre>
</div>
<div class="division"></div>
</article>
<article>
<h1 class="titles">LINEAR SEARCH</h1>
<p>This is the most basic type of search, easy to implement
and understand but inefficient for extremely large data
sets. Let’s look at the following implementation:</p>
<div class="code-div">
<pre>
<code class="Jcode">
public class LinearSearch {
public static void main(String[] args) {
int[] array = { 0, 2, 9, 10, 100, -2, -3,
902, 504, -1000, 20, 9923 };
System.out.println(linearSearch(array, 100));
<span class="code-comment">//prints: 100 found at position index: 4</span>
System.out.println(linearSearch(array, -1));
<span class="code-comment">//prints: not found</span>
System.out.println(linearSearch(array, 9923));
<span class="code-comment">//prints: 9923 found at position index: 11</span>
}
private static String linearSearch(int[] array, int toSearch){
String result ="not found";
for (int i=0; i< array.length; i++){
if(array[i]==toSearch){
result = array[i] +" found at position index: " + i;
break;
}
}
return result;
}
}
</code>
</pre>
</div>
<div class="division"></div>
</article>
<article>
<h1 class="titles">BINARY SEARCH</h1>
<p>Binary search is also easy to implement and easy to
understand. We actually make use of binary search without
knowing when searching in a dictionary for a specific word
or in a phone book. Technically it’s not the same but it is
a very similar process.</p>
<p>We open a dictionary more or less by the middle if the
word we are searching for starts in a letter over the
middle, we discard the first half of the dictionary and now
focus more or less by the middle of the second half. If the
word is below the middle of the second half, we discard the
top half and keep applying this same technique until we find
our word</p>
<p>Let’s look at the pseudocode to make this idea clearer:</p>
<ol>
<li>Receive a sorted array of n elements</li>
<li>Compute middle point</li>
<li>If index toSearch is less than array[mid] then
highestIndex = mid -1 (This changes mid)</li>
<li>If index toSearch is greater than array[mid] then
lowestIndex = mid +1</li>
<li>else if index isn't greater nor less than
array[mid], this means it's equal so return 0</li>
<li>If not greater, less nor equal, then it's not in
the array so return -1</li>
</ol>
<div class="code-div">
<pre>
<code class="Jcode">
public class BinarySearch {
private BinarySearch(){ } <span class="code-comment">//Constructor</span>
public static int binarySearch(int toSearch, int[]array){
int lowestIndex = 0;
int highestIndex = array.length -1;
while (lowestIndex <= highestIndex){
int mid = lowestIndex +
(highestIndex - lowestIndex)/2;
if ( toSearch < array[mid])
highestIndex = mid -1;
else if ( toSearch > array[mid])
lowestIndex = mid + 1;
else if (toSearch==array[mid])
return mid;
}
return -1;
}
public static void main(String[] args) {
int [] array = { -100, 1 , 2, 3, 4, 5, 100, 999, 10203};
System.out.println(binarySearch(999, array)); <span
class="code-comment">//returns index 6</span>
System.out.println(binarySearch(-100, array)); <span
class="code-comment">//returns index 0</span>
System.out.println(binarySearch(6, array)); <span
class="code-comment">//returns index -1</span>
}
}
</code>
</pre>
</div>
<p>Java implements its own .binarySearch() static methods
in the classes Arrays and Collections in the standard
java.util package for performing binary searches on Java
arrays and on Lists, respectively. They must be arrays of
primitives, or the arrays or Lists must be of a type that
implements the Comparable interface, or you must specify a
custom Comparator object.</p>
<div class="division"></div>
</article>
</div>
<!-- ======== /MAIN ======== -->
<script src="js/jquery-1.11.2.min.js"></script>
<script src="js/script.js"></script>
</body>
</html>