Menu DaniWeb
Log In Sign Up
  • Read
  • Contribute
  • Meet
  1. Home
  2. Programming Forum
  3. Software Development Forum
  4. Code Snippet Repository
  5. Reusable Code Snippet

Linear search

16 Years Ago sahasrara 0 Tallied Votes 145 Views Share

Hi friens
Linear search is a way of finding an element in the array that we made it . Binary search is quicker than Linear search .

in this code if key was found , it is removed from array and then sorted array is shown again.

Good Luck!

c++
//Linear search 
//Programming by : Erfan Nasoori
//Mail : ketn68@yahoo.com
//Date of send : 2009/2/1 

 
#include<iostream>
#include<conio>

int linearsearch(int[],int,int);
void BubbleSort(int[],int);

void main()
{
 int *a,i,k;
 int n = 100;
 a = new int[n];

 int index,j,temp;
 cout<<"Enter number (if you enter 0 , entering will be ended :\n";
 cin>>temp;
 for(index=0 ; temp != 0 ; index++)
 {
    a[index] = temp;
    cin>>temp;
 }

 BubbleSort(a,index);

 cout<<"\nTHE SORTED ARRAY IS:\n";
 for(i=0 ; i<index ; ++i)
    cout<<a[i]<<endl;

 cout<<"\nEnter the key word:";
 cin>>k;

 int key = linearsearch(a,index,k);

 if(key != -1)    //  statement of delete found number 
    for(j=key ; j<index ; ++j)
       a[j] = a[j+1];

 for(i=0 ; i<index-1 ; ++i)
    cout<<a[i]<<endl;;

 getch();
}

///////////////////////////////////////////////////////////////////////////////

void BubbleSort(int a[100],int y)
{
 int i,j,hold;
 for(i=1;i<=y-1;++i)
  for(j=0;j<y-i;++j)
   if(a[j]>a[j+1])
   {
     hold=a[j];
     a[j]=a[j+1];
     a[j+1]=hold;
   }
}

////////////////////////////////////////////////////////////////////////////////

int linearsearch(int a[],int m,int k)
{
 for(int i=0 ; i<m ; ++i)
  if(a[i]==k)
     return i;
  return -1;

}
About the Author
Member Avatar for sahasrara
sahasrara 0 Newbie Poster
Be the first to reply
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.

Sign Up — It's Free!
Related Topics
  • Member Avatar Linear Search Template 3
  • Member Avatar C++ Extracting data from text files 24
  • Member Avatar Size of a File Folder/Directory (Python) 6
  • Member Avatar C++ Help with binary search and sort! 12
  • Member Avatar Morse Code 1
  • Member Avatar C++ CLR - How to search hex in a binary file 5
  • Member Avatar Casting Help 2
  • Member Avatar C++ newbie Need help^_^ 5
  • Member Avatar Image and ImageTk weirdness 4
  • Member Avatar Search Array for word Pattern 8
  • Member Avatar SAX API Packages 5
  • Member Avatar c++ source code problem urgent!!!! 26
  • Member Avatar how i can do : addition & sub & multi two Hugo Integer! 11
  • Member Avatar C++ Compiling Error. Please Help! 5
  • Member Avatar Adding rows to a datagrid. 4
  • Member Avatar Search a word, return the line 3
  • Member Avatar Binary search 2
  • Member Avatar Search for a word in a dictionary 4
  • Member Avatar C++ Assignment : Students 5
  • Member Avatar c,c++ socket application in window with open ssl 6
Not what you need?

Reach out to all the awesome people in our software development community by starting your own topic. We equally welcome both specific questions as well as open-ended discussions.

Start New Topic
Topics Feed
Reply to this Topic
Edit Preview

Share Post

Insert Code Block

  • Forums
  • Forum Index
  • Hardware/Software
    • Recommended Topics
  • Programming
    • Recommended Topics
  • Digital Media
    • Recommended Topics
  • Community Center
    • Recommended Topics
  • Latest Content
  • Newest Topics
  • Latest Topics
  • Latest Posts
  • Latest Comments
  • Top Tags
  • Topics Feed
  • Social
  • Top Members
  • Meet People
  • Community Functions
  • DaniWeb Premium
  • Newsletter Archive
  • Markdown Syntax
  • Community Rules
  • Developer APIs
  • Connect API
  • Forum API Docs
  • Tools
  • SEO Backlink Checker
  • Legal
  • Terms of Service
  • Privacy Policy
  • FAQ
  • About Us
  • Advertise
  • Contact Us
© 2025 DaniWeb® LLC