2
Question Question 2
Differentiate between the following.
i. Array Declaration and Initialisation
Array declaration tells the compiler about the size and data type of the array so that the compiler can reserve the required memory for the array. This reserved memory is still empty. Array Initialisation assigns values to the array elements i.e. it stores values in the memory reserved for the array elements.
ii. int a[10] and char a[10]
int a[10] is an array of int data type that can hold 10 integer values whereas char a[10] is an array of char data type that can hold 10 characters.
iii. Sorting and Searching
| Sorting | Searching |
|---|---|
| Sorting means to arrange the elements of the array in ascending or descending order. | Searching means to search for a term or value in an array. |
| Bubble sort and Selection sort are examples of sorting techniques. | Linear search and Binary search are examples of search techniques. |
iv. Linear search and Binary search
| Linear Search | Binary Search |
|---|---|
| Linear search works on sorted and unsorted arrays | Binary search works on only sorted arrays (ascending or descending) |
| Each element of the array is checked against the target value until the element is found or end of the array is reached | Array is successively divided into 2 halves and the target element is searched either in the first half or in the second half |
| Linear Search is slower | Binary Search is faster |
v. Selection sort and Bubble sort
| Selection sort | Bubble sort |
|---|---|
| Selection Sort selects the smallest element from unsorted sub-array and swaps it with the leftmost unsorted element. | Bubble Sort compares adjacent elements and swaps them if they are in wrong order. |
| Performs lesser number of swaps to sort the same array relative to Bubble Sort | Performs more number of swaps to sort the array |
| Selection Sort is faster | Bubble Sort is slower |