You are on page 1of 5

SORTING AND SEARCHING ARRAYS

Suppose you have an array of values. You might want the array values to
be ordered in some way. For example, you might want to order an array of
numbers from the lowest to highest or from highest from lowest, or you might
want to arrange an array of strings into alphabetical order. Arranging a collection
of items into a particular order is called sorting. Typically, we sort arrays into
ascending or descending order.
In this section, we will discuss and implement a simple sorting algorithm.
We will present this algorithm as a way to sort an array of integers. However,
with only minor changes, it can be adapted to sort an arrays of any type that can
be ordered. For example, we could sort an array of employee records according
to identification numbers.
We also will consider searching a given array for a particular entry. We can
search an array whose entries are sorted or one whose entries are completely
unorganized. Both sorting and searching are quite important tasks, and efficient
algorithms are important. We will, however, just introduce you to these topics
Selection Sort
Imagine an array of integers that we want to sort into ascending order. That is,
we will rearrange the value in the indexed variables of the array so that
a[0] < a[1] < a[2] < < a[a.length 1]
We will discuss one of the easiest of the sorting algorithms to understand, the
selection sort. This algorithm follows from the specification of what we want
the algorithm to do. That is, we want the algorithm to rearrange the values of the
array a so that a[0] is the smallest, a[1] is the next smallest, and so forth. That
desire leads to the following pseucode:
For (index = 0; index <a.length; index++)
Place the (index + 1)th smallest element in a[index]
We want this algorithm to use only the one array a. thus, the only way we
can move an array element without losing any of the other elements is to make
it swap places with another element of the array. Any sorting algorithm that
swaps, or interchanges, values is called an interchange sorting algorithm.
Lets start with an example to see how the array elements are
interchanged. Figure 7.5 shows how an array is sorted by interchanging values.
Beginning

With an array of unsorted values, we locate the smallest value in the array.
In our example, this value is the 3 in a[4]. Since we want this smallest value to
be first in the array, we interchange the value in [4] with the value in a[10]. After
that interchange, the smallest value is in a[0] where it belongs. The next
smallest value in the array is the 5 in a[6]

Searching an Array
The sequential search algorithm is very simple and straightforward: it
looks at the array elements from the first to last to see whether the sought-after
item is equal to any of the array elements. The search ends when either the
desired item is found in the array or the end of the array is reached without
finding the item. In the latter case, of course, the search is unsuccessful. If the

array is partially filled, the search considers only the portion of the array that
contains meaningful values

Bucket sort
Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of
an array into a number of buckets. Each bucket is then sorted individually, either using a different
sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a distribution sort,
and is a cousin of radix sort in the most to least significant digit flavour. Bucket sort is a
generalization of pigeonhole sort. Bucket sort can be implemented with comparisons and
therefore can also be considered a comparison sort algorithm. The computational
complexity estimates involve the number of buckets.
Bucket sort works as follows:
1. Set up an array of initially empty "buckets".
2. Scatter: Go over the original array, putting each object in its bucket.
3. Sort each non-empty bucket.
4. Gather: Visit the buckets in order and put all elements back into the original array.

Merge sort
In computer science, merge sort (also commonly spelled mergesort) is an efficient,
general-purpose, comparison-basedsorting algorithm. Most implementations produce a stable
sort, which means that the implementation preserves the input order of equal elements in the
sorted output. Mergesort is a divide and conquer algorithm that was invented by John von
Neumann in 1945. A detailed description and analysis of bottom-up mergesort appeared in a
report by Goldstine and Neumann as early as 1948.

Bubble sort
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that
repeatedly steps through the list to be sorted, compares each pair of adjacent items
and swaps them if they are in the wrong order. The pass through the list is repeated until no
swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison
sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the
algorithm is simple, it is too slow and impractical for most problems even when compared
to insertion sort.[1] It can be practical if the input is usually in sorted order but may occasionally
have some out-of-order elements nearly in position.

DAFTAR PUSTAKA

You might also like