0% found this document useful (0 votes)
2 views

Array

The document discusses searching and sorting in arrays, detailing linear and binary search algorithms, along with their code implementations and best use cases. It also covers various sorting algorithms such as bubble sort, insertion sort, and selection sort, explaining their properties and when to use them. Each algorithm is accompanied by code examples and time complexity analysis.

Uploaded by

sys7379883936
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Array

The document discusses searching and sorting in arrays, detailing linear and binary search algorithms, along with their code implementations and best use cases. It also covers various sorting algorithms such as bubble sort, insertion sort, and selection sort, explaining their properties and when to use them. Each algorithm is accompanied by code examples and time complexity analysis.

Uploaded by

sys7379883936
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Searching in Arrays

Home
Searching in Arrays

One of the basic operations to be performed on an array is searching. Searching an


array means to find a particular element in the array. The search can be used to return
the position of the element or check if it exists in the array.
Linear Search
The simplest search to be done on an array is the linear search. This search starts from
one end of the array and keeps iterating until the element is found, or there are no more
elements left (which means that the element does not exist).
There are no prerequisites for this search to work on an array. It can be used reliably in
any situation
Code for Linear Search
1. #include <stdio.h>
2.
3. int main()
4. {
5. int array[100], search, c, n;
6.
7. printf("Enter number of elements in array\n");
8. scanf("%d", &n);
9.
10. printf("Enter %d integer(s)\n", n);
11.
12. for (c = 0; c < n; c++)
13. scanf("%d", &array[c]);
14.
15. printf("Enter a number to search\n");
16. scanf("%d", &search);
17.
18. for (c = 0; c < n; c++)
19. {
20. if (array[c] == search)
21. {
22. printf("%d is present at location %d.\n", search, c+1);
23. break;
24. }
25. }
26. if (c == n)
27. printf("%d isn't present in the array.\n", search);
28. return 0;
29. }
Best use case
This search is best used when the list of elements is unsorted and the search is to be
performed only once. It is also preferred for list to be small, as the time taken grows with
the size of the data.
Time Complexity

Average: O(n)
Best: O(1)
Worst: O(n)
Average: O(n)
Best: O(1)
Worst: O(n

Binary Search
The linear search approach has one disadvantage. Finding elements in a large array will
be time
consuming. As the array grows, the time will increase linearly. A binary search can be
used as a
solution to this problem in some cases.
The principle of binary search is how we find a page in book. We open the book at a
random page in the middle and based on that page we narrow our search to the left or
right of the book. Indeed, this only is possible if the page numbers are in order.

Binary search
Hence, the prerequisite of performing a binary search is that the array must be sorted.
That is why this works only in cases where keeping a sorted copy of the array is
possible.
The search starts by accessing the middle of the array. If the element is less than this
element, it
starts its search from this element to the left of the array. If the element is larger more
than this
element, it starts its search from this element to the right of the array. This process is
repeated unless a the middle element is equal to the number we are searching.

Code for Binary Search


#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d",&n);

printf("Enter all %d integers in sorted order\n", n);

for (c = 0; c < n; c++)


scanf("%d",&array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("%d is not found in the array.\n", search);

return 0;
}
Best use case
This search is best used when the list of elements is already sorted (not always feasible,
especially when new elements are frequently being inserted). The list to be searched can
be very large without much decrease in searching time, due to the logarithmic time
complexity of the algorithm.
Time Complexity

Sorting in Arrays
Sorting an array means to arrange the elements in the array in a certain order. Various
algorithms
have been designed that sort the array using different methods. Some of these sorts are
more useful than the others in certain situations.
Terminologies
Internal/External Sorting
Internal sorting means that all the data that is to be sorted is stored in memory while
sorting is in progress.
External sorting means that the data is stored outside memory (like on disk) and only
loaded into memory in small chunks. External sorting is usually applied in cases when
data can’t fit into memory entirely, effectively allowing to sort data that does not fit in
the memory.
Stability of Sort
A sorting algorithm is said to be stable if two objects with equal keys appear in the
same order in the sorted output as they appear in the unsorted input.
A sorting algorithm is said to be unstable if there are two or more objects with equal
keys which don’t appear in same order before and after sorting.

Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the
adjacent
elements if they are in wrong order. The pass through the list is repeated until the list is
sorted.
#include <stdio.h>
int main()
{
int arr[100], n, i, j, temp;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);


for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
for (i = 0 ; i < n - 1; i++)
{
for (j = 0 ; j < n - i - 1; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("Sorted list in ascending order:\n");

for (i = 0; i < n; i++)


printf("%d\n", arr[i]);

return 0;
}

Properties

Best use case


This is a very elementary sort which is easy to understand and implement. It is not
recommended in actual production environments. No external memory is required to
sort as it is an in-place sort.

Insertion Sort
In insertion sort, every iteration moves an element from unsorted portion to sorted
portion until all theelements are sorted in the list. An analogy of insertion sort is the
sorting of a deck of cards with our hands. We select one card from the unsorted deck
and put it in the right order in our hands, effectively sorting the whole deck.
Steps
Steps
1. Assume that first element in the list is in its sorted portion of the list and remaining all
elements are in unsorted portion.
2. Take the first element from the unsorted list and insert that element into the sorted list
in order
specified (ascending or descending).
3. Repeat the above process until all the elements from the unsorted list are moved into
the sorted
list.
Code for Insertion Sort
#include<stdio.h>

int main()
{
int data[100],n,temp,i,j;
printf("Enter number of elements to be sorted:");
scanf("%d",&n);
printf("Enter elements: ");
for(i = 0; i < n; i++)
scanf("%d",&data[i]);
for(i = 1; i < n; i++)
{
temp = data[i];
j = i - 1;
while(temp < data[j] && j>=0)
{
data[j + 1] = data[j];
j = j - 1;
}
data[j + 1]=temp;
}
printf("Sorted array: ");
for(i = 0; i < n; i++)
printf("%d ",data[i]);
return 0;
}
inary Search

Properties
Properties
Properties

Best use cases

Best use case


Although this is a elementary sort with the worst case of O(n^2), it performs much better
when the array is nearly sorted, as lesser elements would have to be moved. It is also
preferred when the number of elements are less as it has significantly less overhead
than the other sorts. It consumes less memory and is simpler to implement.
In some quick sort implementations, insertion sort is internally to sort the smaller lists
faster.
Selection Sort
Selection sort is generally used for sorting files with very large records and small keys. It
selects the smallest (or largest) element in the array and then removes it to place in a
new list. Doing this multiple times would yield the sorted array.
Steps
Steps
1. Select the first element of the list.
2. Compare the selected element with all other elements in the list.
3. For every comparison, if any element is smaller (or larger) than selected element,
swap these
two elements.
4. Repeat the same procedure with next position in the list till the entire list is sorted.

Code for Selection Sort


#include <stdio.h>

int main()
{
int array[100], n, pos, temp, i, j;
printf("Enter number of elements\n");
scanf("%d", &n);

printf("Enter the %d values\n", n);

for (i = 0; i < n; i++)


scanf("%d", &array[i]);

for (i = 0; i < (n - 1); i++)


{
pos = i;

for (j = i + 1; j < n; j++)


{
if (array[pos] > array[j])
pos = j;
}
if (pos != i)
{
temp = array[i];
array[i] = array[pos];
array[pos] = temp;
}
}

printf("Sorted list in ascending order:\n");

for (i = 0; i < n; i++)


printf("%d\n", array[i]);

return 0;
}
Properties

Best use case

Best use case


This sort is not influenced by the initial ordering of elements in the array and can be
sued to efficiently sort small lists. It performs the least amount of data movement
amongst all sorts, therefore it could beused where data manipulation is costly.

You might also like