Wednesday, December 12, 2018

Sorting and Searching



Sorting Defination:
it needs to speed up searching operation in a list.

There is 2 Type of Sorting:
-          Ascending
-          Descending

And there is 2 difficulties of sorting:
Simple:
                        Bubble sort
      Selection sort
      Insertion sort
Intermediate:
 Quick Sort
                   Merge Sort

Sorry But in this blog we only learn simple sorting because i also still learning about it

Syntax for Bubble Sort:
void Bubble(int *DataArr, int n)
{
                        int i, j;
                        for(i=1; i<n; i++)
                                    for(j=n-1; j>=i; j--)
                                                if(DataArr[j-1] > DataArr[j])
                         Swap(&DataArr[j-1],&DataArr[j]);
}

Syntax for  Selection Sort:
for(i=0; i<N-1; i++){      /* N=number of data */
            Set idx_smallest equal to i
            for(j=i+1; j<N; j++){
                        If array[ j ] < array [ idx_smallest ] then idx_smallest = j
    }
            Swap array[ i ] with array[ idx_smallest ]
}

Syntax for Insertion Sort:
for(i=1; i<n; i++) {
               x = A[i], insert x to its suitable place between A[0] and A[i-1].
}

So thats all for now untill the next update for the intermidiate one hehehe..

Searching Defination:
Searching is an action to retrieve information based on particular key from some saved information

Several types of searching algorithm:
1.      Linear Search
Linear search compares each element of the array with the search key.

2.      Binary Search:
                        The linear searching method works well for small or unsorted arrays. However, for large arrays linear searching is inefficient

3.      Interpolation Search
            technique is performed on the sorted data, This searching process is almost similar with binary search technique, and This searching technique is done with the approximate location of the data


Syntax for Linear Search:
1. n : total record of array x.
2. For each x[i], 0 £  i £ n-1, check whether x[i] = key.
3. If x[i] = key, then the searched data is found in index=i. Finished.
4. If x[i] ¹ key, then continue searching until the last data which is i = n-1.
5. If i= n-1 and x[i] ¹ key, it means the data is not exist in the list, and set index = -1. Finished.

Syntax for Binary Search:
1. n : total record of array x.
2. left=0,  right= n-1.
3. mid =(int) (left + right)/2.
4. If x[mid]=key then index = mid. Finished.
5. If x[mid]<key then left = mid+1.
6. If x[mid]>key then right = mid-1.
7. If left £ right and x[mid] ¹ key, then repeat point 3.
8. If x[mid] ¹ key then index = -1. Finished.

Syntax for Interpolation Search:
1.      In the interpolation search, we'll split the data according to the following formula:

2.      If data[mid] = sought data, data has been found, searching is stopped and return mid.
3.      If data[mid]!= sought data, repeat point **
4.      **Searching is continued while sought data > data[min] and sought data < data[max].
5.      Looking for a mid value by entering into the interpolation formula
6.      If data[mid] > sought data, high = mid – 1
7.      If data[mid] < sought data, low = mid + 1
8.      It will be looped until the requirements point ** are not met then return (-1), data not found



1 comment:

  1. Excellent post! Thankyou for sharing a valuable comments on a particular concept. Other wise If any one who want to learn c plus contact us on 9311002620 or visit :- https://htsindia.com/Courses/embedded-systems-and-robotics/cplusplustraininginstituteinsouthdelhi

    ReplyDelete