Saturday, December 30, 2017

Data Structure Sorting Technique For BCA , MCA and All IT Fields



--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah - HOD (IT)
Contact No : 7600044051
E-Mail : rupareleducation@gmail.com


Bubble Sort

This sorting method is Based on comparing and exchanging pairs of adjacent elements, until elements in an area are sorted. One round of scanning through the set of elements of any array called  a  “Pass”.

The Bubble sort method derives its name from the fact that the smallest data item “Bubble Up” to the top of an array of n elements. Assuming that the array elements are arranged vertically. The algorithm begins by comparing the bottom most elements with its adjacent Element and an exchange of the two elements take place of bottom most element is smaller then the adjacent element.

Bubble Sort Algorithm

1.         Start
2.         Bubble_sort(n, array)
            n  is Number of Elements
            array is List of array elements
3.         initialize
            i = 0
4.         Repeat through step – 7 while ( i < n)
5.         j = 0
6.         Repeat through step – 7 while (i < n-1)
7.         Check for Execution
            if array[j] < array [j-1] then
                        temp = array[j]
                        array[j] = array[j+1]
                        array[j+1] = temp
8.         j = j + 1
9.         i = i + 1
10.       Exit

---


Selection Sort
Selection sort is the easiest way to sort a table. Beginning with the first record in the table, a search is performed to locate which has the smallest key. When the element is found it is interchange with the first record. The interchange place the record with the smallest key in the first position of the table. A search for the second smallest is then carried out. This is accomplished by examining the keys of the records form the second element on word. The element which has second smallest key is interchange with the element located in the second position of the table. The process of searching for the record with the next smallest key and placing it in its proper position continues until all records have been sorted.

Selection Sort Algorithm

1.         Start
2.         Selection_sort(n, array)
            n  is Number of Elements
            array is List of array elements
3.         initialize
            i = 0
4.         Repeat through step – 7 while ( i < n)
5.         j = i + 1
6.         Repeat through step – 7 while (i < n-1)
7.         Check for Execution
            if array[i] < array [j] then
                        temp = array[i]
                        array[i] = array[j]
                        array[j] = temp
8.         j = j + 1
9.         i = i + 1
10.       Exit

---


 Merge Sort
The operation on sorting is closely related to the process of merging. First examine the merging of two ordered tables which can be combined to produce a single sorted element in table. This process can be accomplished easily by successively selecting the records with the smallest key occurring either of the table or placing this record in a new table there be creating order list.
In merge sort there are two different array in unsorted elements so first of all arrange in orderly and then compare each elements in an ordered array and transfer smallest element in third merge array in sorted order.

Merge Sort Algorithm

1.         Start
2.         Merge_sort(a, array1, b, array2, array3)
            where  a           Represent No. of Element in First Array
                        array1  Represent the list of element in First Array
                        b          Represent No. of Element in Second Array
                        array2  Represent the list of element in Second Array
                        array3  Empty Array
3.         Initialize
            i = 0, j = 0, k = 0
4.         Repeat Step 5 While (( i < a) and (j < b))
5.         if  array1[i] < array2[j] then
                        Array3[k] = array1[i]
                        i  = i + 1
                        k = k + 1
            else if array1[i] > array2[j] then
                        Array3[k] = array1[j]
                        j  = j + 1
                        k = k + 1
            else [ Elements of Both List are equal and insert only
one of them into Third Array]
Array3[k] = array1[j]
i  = i  + 1
                        j  = j + 1
                        k = k + 1
6.         Exit

---

Insertion Sort

1.      In insertion sort technique scan the list from first to last and find minimum value from it.
2.      And then inserting each element into its proper position in the previously sorted sub list.
3.      It is very simple and efficient algorithm technique to sort an element from the list.
4.      It is very simple mechanism.
5.      It just take element from the list one by one and compare their following element form the list.
6.      The insertion algorithm technique occurs by inserting at their proper position
7.      In the first repetition process first of all second element is compare with first element of an array.
8.      At the time of comparison if process  found higher element then it interchange the position of that element and the process are going on same manner.
9.      The example of insertion sort is occurs while playing a cards and this process is repeated until all the cards are not set in proper place. 

Insertion Sort Algorithm

1.         Start
2.         insertion_sort(array, n)
            Where array    Represent the list of elements
                         n         Represent the Number of elements in an array
3.         Declare  i=0, j, x, temp
4.         Search Smallest Element and set as top of the position in an array.
5.         Repeat Through Step – 7  while (i < n )
6.         x = array[i]   and j = i-1  repeat through Step – 7 while (j > 0 and x < a[j])
7.         Interchange Value :
            a[j+1]  = a[j]
8.         j--
9.         a[j+1] = x  and i++
10.       Exit.   

---

Quick Sort

This method is used to perform sorting on larger table. The goal is to place a particular record in it’s final position within the table. In so doing all records which come first this record have smaller keys, while all records that follow it have larger keys. This technique essentially partitions the table into sub table. The same process can then be applied each of these sub table and repeated until all the records are placed in their positions.

 Quick Sort Algorithm

1.         Start
2.         Qick_sort(array,first,last)
            Where array    Represent the list of an Elements
                        first      Represent the position of the first element in the list
                        last       Represent the position of the last  element in the list
3.         Initialize
            low = first
            high = last
            mid = array[(low+high)/2]
4.         Repeat through step 10 while
            (low <= high)
5.         Repeat Step – 6
while (array[low] < mid)
6.         low = low + 1
7.         while (array[high] > mid)
8.         high = high – 1
9.         if(low <= high)
            temp  = array[low]
            array[low] = array[high]
            array[high] = temp
            low = low + 1
            high = high – 1
10        if(first < high)
            Qick_sort(array,first,high)
11        if(low < last)
            Qick_sort(array,low,last)
12.       Exit

---

Bucket Sort
  • Bucket Sort is a sorting method that can be use to sort a list of number by it’s base.
  • It means it sub divide the given data into the various bucket depend on certain character order.
  • For eg. If user want to sort a list of English word on it’s base there are only 26 bucket use to sort the word
  • Bucket sort is also known as bin sort.
  • The sorting algorithm that work into a partition of an array in a number of bucket.
  • It is recursive process to distribute each and every number into sorted list the base of bucket of 0 to 9
  • Following is some steps to follow the bucket sort.
1.         Setup an array with initially empty Buckets.
2.         Scatter Original array and put each object into the bucket.
3.         Sort each non – empty element into the bucket.
4.         Gather visited buckets into the proper order and do all the process
repeatedly.


Bucket Sort Algorithm
1.         Start
2.         bucketsort(array,n)
            where array                Represent a list of an array
                        n                      Represent number of element in a array list
3.         Declare  i = 1, j, flag=0, position
4.         bucket   -> New array of an Empty List
5.         Repeat Step 6 ,7 and 8 while flag != n
6.         check  (array [j] / position )%10  = 0 then
                        Bucket[index]  = array[j]
                        Increment in flag by 1
7.         check  flag = n then
            Print Sorted List
8.         Transfer all Bucket index into an array
9.         Stop.

---

Shell Sort
  • Shell Sort is introduce to improve the efficiency of simple insertion sort.
  • Shell sort is also call dimension increment sorting.
  • It work on in place comparison method.
  • It is generalize and exchange it’s element with their elements in the list.
  • Shell sort sorting algorithm is almost similar to bubble sort algorithm.
  • Shell sort compare their elements in a certain distance away from each other elements repeatedly in the list.
  • It is also require recursive process.

Shell Sort Algorithm
1.         Start
2.         shellsort (array, n)
            where array                Represent a list of an array
                        n                      Represent number of element in a array list
3.         Declare i, j, temp and gap as integer
4.         Divide the list into smaller sub list of Equal interval
Initialize gap  size /2
5.         Repeat Step 7 and 8 while  gap > 0  
6.         Sort Sub list using insertion sort technique.
initialize i as gap
7.         check j = i until j>=gap   then
            Replace the value at index j with the value at index j-gap
8.         else  Replace the value at index j with temp and increase i by one a
9.         Stop.



J Thank you J