You are on page 1of 14

LECTURE-23

PROGRAMMING IN ‘C’
 LINEAR SEARCH:
 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 ( Array A, Value x)


 Step 1: Set i to 1
 Step 2: if i > n then go to step 7
 Step 3: if A[i] = x then go to step 6
 Step 4: Set i to i + 1
 Step 5: Go to Step 2
 Step 6: Print Element x Found at index i and go to step 8
 Step 7: Print element not found
 Step 8: Exit
 BINARY SEARCH
 Search a sorted array by repeatedly dividing the search interval in half.
Begin with an interval covering the whole array. If the value of the
search key is less than the item in the middle of the interval, narrow the
interval to the lower half. Otherwise, narrow it to the upper half.
Repeatedly check until the value is found or the interval is empty.

 ALGORITHM:
 Step 1 : Find the middle element of array. using ,
 middle = initial_value + end_value / 2 ;
 Step 2 : If middle = element, return ‘element found’ and index.
 Step 3 : if middle > element, call the function with end_value = middle -
1.
 Step 4 : if middle < element, call the function with start_value = middle
+1.
 Step 5 : exit.
 BUBBLE SORT
 Bubble Sort in C is a sorting algorithm where we repeatedly iterate through
the array and swap adjacent elements that are unordered. We repeat this until
the array is sorted.
 As an example, for the array mentioned above - [5, 1, 4, 2, 3] we can see that
5 should not be on the left of 1 and so, we swap them to get: [1, 5, 4, 2, 3].
Next, we see that 5 should again not be on the left of 4. We swap 5 and 4 to
get [1, 4, 5, 2, 3]. We repeat this for 5 and 2 and subsequently for 5 and 3 to
get [1, 4, 2, 3, 5].
 Step 1: Repeat Steps 2 and 3 for i=1 to 10
 Step 2: Set j=1
 Step 3: Repeat while j<=n
 (A) if a[i] < a[j]
 Then interchange a[i] and a[j]
 [End of if]
 (B) Set j = j+1
 [End of Inner Loop]
 [End of Step 1 Outer Loop]
 Step 4: Exit
 INSERTION SORT
 Insertion Sort is a sorting algorithm where the array is sorted by taking
one element at a time. The principle behind insertion sort is to take one
element, iterate through the sorted array & find its correct position in
the sorted array. Insertion Sort works in a similar manner as we arrange
a deck of cards.

 Step 1 − If the element is the first one, it is already sorted.


 Step 2 – Move to next element
 Step 3 − Compare the current element with all elements in the sorted
array
 Step 4 – If the element in the sorted array is smaller than the current
element, iterate to the next element. Otherwise, shift all the greater
element in the array by one position towards the right
 Step 5 − Insert the value at the correct position
 Step 6 − Repeat until the complete list is sorted
 SELECTION SORT
 Selection sort is another algorithm that is used for sorting.
This sorting algorithm iterates through the array and finds the
smallest number in the array and swaps it with the first
element if it is smaller than the first element. Next, it goes on
to the second element and so on until all elements are sorted.

 Step 1 − Set min to the first location


 Step 2 − Search the minimum element in the array
 Step 3 – swap the first location with the minimum value in the
array
 Step 4 – assign the second element as min.
 Step 5 − Repeat the process until we get a sorted array.
 FIND THE SQUARE OF A NUMBER
 Step 1 → Define value n to find square root of
 Step 2 → Define variable i and set it to 1 (For integer

part)
 Step 3 → Define variable p and set it to 0.00001 (For

fraction part)
 Step 4 → While i*i is less than n, increment i
 Step 5 → Step 4 should produce the integer part so far
 Step 6 → While i*i is less than n, add p to i
 Step 7 → Now i has the square root value of n
 ARRAY ORDER REVERSAL
 In this algorithm, subarrays are created and reversed to
perform the rotation of the array. Subarrays are created,
rotated individually and then joined together and reversed
back to get the rotated array.Input : array arr[] , positions
that are needed to be rotated r , length of array n.

 Step 1: Split the array into two sub arrays of 0 - (d-1) and
d - (n-1) size, a1 [d] and a2[n-d];
 Step 2: Reverse both arrays using the reverse method.
 Step 3: Join a1 and a2 back to get an array of original size.
 Step 4: Reverse this joined array to get the rotated array.
 Step 5: Print the array using the standard output method.
 REVERSAL OF A STRING 
 Step 1: Define a string and another empty string to store the reversed string.
 Step 2: Now, iterate the string from last and store a character from back into variable revString.
 Step 3: At the end of the loop, revString will hold the reverse of the string.

 Example:
 #include <stdio.h>
 #include <string.h>
 int main()
 { char string[] = "Dream big"; //Stores the reverse of given string
 char reversedStr[strlen(string)];
 int j = 0; //Iterate through the string from last and add each character to variable reversedStr
 for(int i = strlen(string)-1; i >= 0; i--){
 reversedStr[j++] = string[i];
 }
 reversedStr[j] = '\0';
 printf("Original string: %s", string); //Displays the reverse of given string
 printf("\nReverse of given string: %s", reversedStr);
 return 0;
 }

You might also like