Linear Search
Linear Search
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
int main(){
int a[TOTAL_SIZE] = {10, 20, 30, 40, 50}, current_size = 5, i = 0, n;
int key = 0;
print_array(a, current_size);
key = 10;
//key = 15;
for(i=0; i<current_size ; i++){
if(a[i] == key){
printf("Found\n");
}
}
return 0;
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Break Once Element Found
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
int main(){
int a[TOTAL_SIZE] = {10, 20, 10, 40, 50}, current_size = 5, i = 0, n;
int key = 0;
print_array(a, current_size);
key = 10;
//key = 15;
for(i=0; i<current_size ; i++){
if(a[i] == key){
printf("Found\n");
break;
}
}
return 0;
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Test Element Not Found
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
int main(){
int a[TOTAL_SIZE] = {10, 20, 30, 40, 50}, current_size = 5, i = 0, n;
int key = 0, flag = 0;
print_array(a, current_size);
key = 10;
//key = 15;
for(i=0; i<current_size ; i++){
if(a[i] == key){
printf("Found\n");
flag = 1;
}
}
if(flag==0){
printf("Not Found\n");
}
return 0;
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Search Using Flag
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
int main(){
int a[TOTAL_SIZE] = {10, 20, 30, 40, 50}, current_size = 5, i = 0, n;
int key = 0, flag = 0;
print_array(a, current_size);
key = 10;
//key = 15;
for(i=0; i<current_size ; i++){
if(a[i] == key){
flag = 1;
}
}
if(flag==1){
printf("Found\n");
}
else{
printf("Not Found\n");
}
return 0;
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Search Using Index
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
int main(){
int a[TOTAL_SIZE] = {10, 20, 30, 40, 50}, current_size = 5, i = 0, n;
int key = 0, index = -1;
print_array(a, current_size);
key = 10;
//key = 15;
for(i=0; i<current_size ; i++){
if(a[i] == key){
index = i;
}
}
if(index==-1){
printf("Not Found\n");
}
else{
printf("Found\n");
}
return 0;
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Search Using Function
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
void search_array(int *array, int current_size, int key);
int main(){
int a[TOTAL_SIZE] = {10, 20, 30, 40, 50}, current_size = 5, i = 0, n;
int key = 0, flag = 0;
print_array(a, current_size);
search_array(a, current_size, key = 10);
search_array(a, current_size, key = 15);
return 0;
}
void search_array(int *array, int current_size, int key){
int i;
for(i=0; i<current_size ; i++){
if(array[i] == key){
printf("Found\n");
return;
}
}
printf("Not Found\n");
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Return Truth Value from Search Function
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
int search_array(int *array, int current_size, int key);
int main(){
int a[TOTAL_SIZE] = {10, 20, 30, 40, 50}, current_size = 5, i = 0, n;
int key = 0, flag = 0;
print_array(a, current_size);
if(search_array(a, current_size, key = 10)){
printf("Found\n");
}else{
printf("Not Found\n");
}
if(search_array(a, current_size, key = 15)){
printf("Found\n");
}else{
printf("Not Found\n");
}
return 0;
}
int search_array(int *array, int current_size, int key){
int i;
for(i=0; i<current_size ; i++)
if(array[i] == key)
return 1;
return 0;
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Return Index from Search Function (Recommended)
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
int linear_search(int *array, int current_size, int key);
int main(){
int a[TOTAL_SIZE] = {10, 20, 30, 40, 50}, current_size = 5, i = 0, n;
int key = 0, flag = 0;
print_array(a, current_size);
if(linear_search(a, current_size, key = 10)!=-1){
printf("Found\n");
}else{
printf("Not Found\n");
}
if(linear_search(a, current_size, key = 15)!=-1){
printf("Found\n");
}else{
printf("Not Found\n");
}
return 0;
}
int linear_search(int *array, int current_size, int key){
int i;
for(i=0; i<current_size ; i++)
if(array[i] == key)
return i;
return -1; // -1 Indecate Invalid Index, i.e. Key Element Not Found
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Recursive Linear Search
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
int recursive_linear_search(int *array, int current_size, int key);
int main(){
int a[TOTAL_SIZE] = {10, 20, 30, 40, 50}, current_size = 5, i = 0, n;
int key = 0, flag = 0;
print_array(a, current_size);
if(recursive_linear_search(a, current_size, key = 10)!=-1){
printf("Found\n");
}else{
printf("Not Found\n");
}
if(recursive_linear_search(a, current_size, key = 15)!=-1){
printf("Found\n");
}else{
printf("Not Found\n");
}
return 0;
}
int recursive_linear_search(int *array, int current_size, int key){
if(current_size < 0)
return -1; // -1 Indecate Invalid Index, i.e. Key Element Not Found
else if(array[current_size] == key)
return current_size;
return recursive_linear_search(array, current_size-1, key);
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Recursive Linear Search (Alternative Approach)
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
int recursive_linear_search(int *array, int start, int end, int key);
int main(){
int a[TOTAL_SIZE] = {10, 20, 30, 40, 50}, current_size = 5, i = 0, n;
int key = 0, flag = 0;
print_array(a, current_size);
if(recursive_linear_search(a, 0, current_size, key = 10)!=-1){
printf("Found\n");
}else{
printf("Not Found\n");
}
if(recursive_linear_search(a, 0, current_size, key = 15)!=-1){
printf("Found\n");
}else{
printf("Not Found\n");
}
return 0;
}
int recursive_linear_search(int *array, int start, int end, int key){
if(start > end)
return -1; // -1 Indecate Invalid Index, i.e. Key Element Not Found
else if(array[start] == key)
return start;
return recursive_linear_search(array, start+1, end, key);
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Last Element Index for Multiple Occurrences of Element
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
int search_last_element_index(int *array, int current_size, int key);
int main(){
int a[TOTAL_SIZE] = {10, 20, 30, 10, 50}, current_size = 5, i = 0, n;
int key = 0, flag = 0;
print_array(a, current_size);
printf("%d\n", search_last_element_index(a, current_size, key = 10)); // 3
printf("%d\n", search_last_element_index(a, current_size, key = 15)); // -1
return 0;
}
int search_last_element_index(int *array, int current_size, int key){
int i, last_index = -1;
for(i=0; i<current_size; i++){
if(array[i] == key){
last_index = i;
}
}
return last_index;
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Last updated