You are on page 1of 6

Alogrithm:1

Linear Search Algorithm:

Linear search is a simple searching algorithm. It sequentially checks each element of


the list until a match is found or the whole list has been searched.
1. Start at the first element of the list.
2. Compare the current element with the target value.
• If they are equal, you have found the target, and the search is
successful.
• If not, move to the next element.
3. Repeat this process until you find the target or reach the end of the list.
4. If the target is not found after checking all elements, the search is
unsuccessful.

Binary Search
Algorithm:
Binary search is a more efficient searching algorithm, but it requires the list to
be
sorted in ascending order.
1. Start with the entire sorted list.
2. Find the middle element of the list.
3. Compare the middle element with the target value.
• If they are equal, you have found the target, and the search is successful.
• If the middle element is less than the target, the target must be in the
right half of the list.
• If the middle element is greater than the target, the target must be in
the left half of the list.

4. Repeat the process on the appropriate half of the list. That is, if the target is in
the left half, repeat the search on the left half; if it's in the right half, repeat the
search on the right half.
5. Continue this process, dividing the search interval in half each time, until you
find the target or determine that it doesn't exist in the list.
Alogrithm:2
1. Start with the entire sorted list.
2. Find the middle element of the list.
3. Compare the middle element with the target value.
• If they are equal, you have found the target, and the search is
successful.
• If the middle element is less than the target, the target must be in the
right half of the list.
• If the middle element is greater than the target, the target must be in
the left half of the list.
4. Repeat the process on the appropriate half of the list. That is, if the target is in
the left half, repeat the search on the left half; if it's in the right half, repeat the
search on the right half.
5. Continue this process, dividing the search interval in half each time, until you
find the target or determine that it doesn't exist in the list.
Alogrithm:3
1. Start with the second element (index 1) assuming the first element is already
sorted.
2. Compare the current element with the previous elements in the sorted
portion of the array.
3. If the current element is smaller than the previous element, shift the previous
element to the right.
4. Continue comparing and shifting elements until you find an element smaller
than the current element or reach the beginning of the array.
5. Insert the current element in its correct position.
6. Repeat the process for all elements, incrementing the position by one in each
iteration.

Alogrithm:4
1. Choose a pivot element from the array.
2. Partition the array into two sub-arrays, such that elements less than the
pivot are on the left, and elements greater than the pivot are on the right.
3. Recursively apply QuickSort to the left and right sub-arrays.
4. Combine the sorted sub-arrays and the pivot back into a single sorted array.

Alogrithm:5
1. Divide the unsorted list into n sub-lists, each containing one element.
2. Repeatedly merge sub-lists to produce new sorted sub-lists until there is
only one sub-list remaining.
Alogrithm:6
1. Start at the beginning of the text.
2. For each position in the text, check if the pattern matches the characters
starting at that position.
3. If a match is found, record the position.
4. Move one position forward in the text and repeat step 2 until the end of the
text is reached.
5. If the entire pattern is matched at a position, you have found an occurrence of
the pattern.
Alogrithm:7
1. Calculate the value-to-weight ratio for each item.
2. Sort the items in descending order based on their value-to-weight ratio.
3. Initialize the total value to 0 and the current weight in the knapsack to 0.
4. Iterate through the sorted items and add items to the knapsack in a greedy
manner.
5. For each item, if adding it to the knapsack doesn't exceed the weight capacity,
add the entire item to the knapsack and update the total value and current
weight.
6. If adding the entire item would exceed the weight capacity, calculate the
fraction of the item that can be added to maximize value without exceeding
the capacity.
7. Continue this process until the knapsack is full or no more items are available.
8. The total value of the items in the knapsack is the solution.

You might also like