Professional Documents
Culture Documents
SORTING
Sorting
particular order.
order.
Sorting
What is Sorting?
Sorting is a process of ordering or placing a list of elements
easier.
Sorting Techniques
Complexity).
Sorting techniques are differentiated by their efficiency
and space requirements.
The complexity of sorting algorithm
Insertion Sort
part.
Values from the unsorted part are picked and placed at the
implementation
In selection sort, the first element in the list is selected and it is compared
If any element is smaller than the selected element (for Ascending order),
then both are swapped so that first position is filled with the smallest element
Next, we select the element at a second position in the list and it is compared
If any element is smaller than the selected element, then both are swapped.
The selection sort algorithm is performed using the
following steps...
Step 1 - Select the first element of the list (i.e., Element at
first position in the list).
Step 2: Compare the selected element with all the other
elements in the list.
Step 3: In every comparision, if any element is found smaller
than the selected element (for Ascending order), then both
are swapped.
Step 4: Repeat the same procedure with element in the next
position in the list till the entire list is sorted.
Selection Sort Algorithm
begin BubbleSort(list)
for i = 1 to size
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
MERGE SORT
Algorithm
Merge sort keeps on dividing the list into equal halves until
it can no more be divided. By definition, if it is only one
element in the list, it is sorted. Then, merge sort combines
the smaller sorted lists keeping the new list sorted too.
array.
• Beg = 1
• End = n = 8
MERGE_SORT(arr, beg, end)
if beg < end
set mid = (beg + end)/2
MERGE_SORT(arr, beg, mid)
MERGE_SORT(arr, mid + 1, end)
MERGE (arr, beg, mid, end)
end of if
END MERGE_SORT
The important part of the merge sort is the MERGE function.
sorted array A[beg…end].
mid, and end.
The implementation of the MERGE function is given as
follows -
/* Function to merge the subarrays of a[] */
void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;
int LeftArray[n1], RightArray[n2]; //temporary arrays
/* copy data to temp arrays */
for (int i = 0; i < n1; i++)
LeftArray[i] = a[beg + i];
for (int j = 0; j < n2; j++)
RightArray[j] = a[mid + 1 + j];
i = 0, /* initial index of first sub-array */
j = 0; /* initial index of second sub-array */
k = beg; /* initial index of merged sub-array */
while (i < n1 && j < n2)
{
if(LeftArray[i] <= RightArray[j])
{
a[k] = LeftArray[i];
i++;
}
else
{
a[k] = RightArray[j];
j++;
}
k++;
}
while (i<n1)
{
a[k] = LeftArray[i];
i++;
k++;
}
while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}
According to the merge sort, first divide the given array into two
equal halves. Merge sort keeps dividing the list into equal parts until
it cannot be further divided.
As there are eight elements in the given array, so it is divided into two
arrays of size 4.
Now, again divide these two arrays into halves.
As they are of size 4, so divide them into new
arrays of size 2.
Now, there is a final merging of the arrays. After the final merging of above
arrays, the array will look like -
What is Searching?
list of values.
collection of items.
Searching Techniques
To search an element in a given array, it can be done in following
ways:
2. Binary Search
Linear Search
• Linear search is a very basic and simple search algorithm. In
Linear search, we search an element or value in a given array
by traversing the array from the starting, till the desired
element or value is found.
• It compares the element to be searched with all the elements
present in the array and when the element is matched
successfully, it returns the index of the element in the array,
else it return -1.
• Linear Search is applied on unsorted or unordered lists, when
there are fewer elements in a list.
Linear search is a very simple search algorithm. In this type of search, a
sequential search is made over all items one by one. Every item is checked and if
a match is found then that particular item is returned, otherwise the search
continues till the end of the data collection.
Linear Search Algorithm
we compare the key element with the mid element. So (25 == 25), hence we have found the key at location
[mid] = 6.
Thus we repeatedly divide the array and by comparing the key element with the mid, we decide in which half
to search for the key. Binary search is more efficient in terms of time and correctness and is a lot faster too.