Professional Documents
Culture Documents
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.
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.
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;
}