Visual C# 2005 Guide
Linear Search

The linear search is the simplest method to search for items stored in an unsorted list. The technique involves comparing the search item with each item in the list until either a match is found or the end of the list is reached.

The algorithm in pseudocode for a linear search is as follows,

CurrentItem ← 1
Found ← false
Repeat
   If List[CurrentItem] = ItemSought
      Then Found ← true
      CurrentItem ← CurrentItem + 1
Until Found = True Or CurrentItem > Max

Having A Go

The Array class in C# has a whole range of methods that mean that it would be rare to need to write a linear search function. A useful one here is,

Array.IndexOf(Array, Object) - returns the index of the first occurence of the object in the array or -1 for a zero-indexed array

However, in order to better understand this and subsequent algorithms, it is a good idea to implement the algorithm yourself. Write a function which accepts an array and an item to look for as parameters. The function should return an integer equal to the location of the item or -1 if the item is not found. Use the list of band names below as test data for your program.

{ "Adem", "Bill Withers", "Chicken Shack", "David Gray", "Devo", "Eurythmics", "Funkadelic", "Jobriath", "Luciano Pavarotti", "Mitch Benn", "Pony Club", "Proclaimers", "Queen", "Rufus Wainwright", "Small World", "Stevie Wonder", "The Blow Monkeys", "The Byrds", "The Chi-lites", "The Orb", "The Strokes", "Thomas Dolby", "Altered Images", "Aretha Franklin", "Athlete", "Blancmange", "Clinic", "Country Joe & the Fish", "Deacon Blue", "Faithless", "INXS", "Jack Johnson", "Jan & Dean", "Kubb", "Led Zeppelin", "Nirvana", "Rezillos", "Spiritualized", "Suede", "Sugababes", "The Associates", "The Clash", "The Cranberries", "The Creation", "The Dead Kennedys", "The flamin groovies", "The Mamas & the Papas", "The Spencer Davis Group", "U2", "UFO", "Wyclef Jean", "Big Audio Dynamite", "Billy Bragg", "Billy Idol", "Bright Eyes", "Chairman Of The Board", "Darden Smith", "Dean Taylor", "Dio", "Dr Feelgood", "Elastica", "Eliza Carthy", "Eric Carmen", "Foo Fighters", "Francoise Hardy", "Jimmy Nail", "Journey", "Judas Priest", "Magazine", "My Chemical Romance", "New Order", "Orange Juice", "Orenda Fink", "Phil Lynott", "Prince", "Ryan Adams & The Cardinals", "Sack", "Specials", "Stereophonics", "The Searchers", "Tones On Tail", "Wonder Stuff", "Depeche Mode", "Dogs Die In Hot Cars", "Free", "Funeral for a Friend", "Hard-Fi", "James Booker", "James Brown", "Joseph Arthur", "Kid Symphony", "Lotus Eaters", "Ned's Atomic Dustbin", "Peter Gabriel", "Procol Harum", "Purple Hearts", "Stiff Little Fingers", "The Magic Numbers", "The Pogues", "The Subways", "Tinariwen", "Tom Vek", "Bob Dylan", "Bob Marley", "Brendan Benson", "Crystal Gayle", "Cyndi Lauper", "Dionne Warwick", "Electric Six", "Georgia Satellites", "House of Love", "Idlewild", "James Yorkston And The Athletes", "Joy Division", "Laura Cantrell", "Ludus", "Lynyrd Skynyrd", "Manic Street Preachers", "Mike Oldfield", "PJ Harvey", "Primitives", "Product G&B", "Roy Buchanan", "Sterephonics", "The Assembly", "The Farm", "The Fraternity Of Man", "The Mission", "The Saints", "The Sonics", "The Troggs", "Zodiac Mindwarp", "Bert Jansch", "Box Tops", "De Dannan", "Echobelly", "Fernhill", "Fugees (Refugee Camp)", "Grandmaster Flash", "Hawkwind", "Inspiral Carpets", "Jeff Beck", "Kasabian", "Laura Veirs", "Martha Wainwright", "OMD", "Pete Seeger", "Pig Bag", "Ramsey Lewis", "Raymonde", "Richard Thompson", "Silly Sisters", "The Bluetones", "The Flys", "The Kinks", "The Levellers", "Tommy James & The Shondells", "Turin Brakes", "Van Halen", "Barret Strong", "Beth Orton", "Bukka White", "Coldplay", "Culture Club", "Erasure", "Explosions In the Sky", "Fairport Convention", "Marvin Gaye", "Michelle Shocked", "Midge Ure", "Neil Young", "Nina Nastasia", "Norah Jones", "Owen Hand", "PWEI", "Rose Royce", "Spacemen 3", "The Bangles", "The Flaming Lips", "The Futureheads", "The Herd", "The Hives", "The Watersons", "Wheatus", "Woody Guthrie", "ABC", "Abraham", "Babybird", "Bauhaus", "Black Sabbath", "Blind Willie Johnson", "Bloc Party", "Bob & Earl", "Capercaillie", "Cliff Bennet & The Rebel Rousers", "Elmore James & His Broom Dusters", "Emiliana Torrini", "Ewan MacColl", "Halloween Alaska", "Hank Williams", "Joe Walsh", "John Prine", "Kathryn Williams", "Magnet", "Monster Magnet", "Odetta", "Portishead", "Sade", "Scott Walker", "Sinéad O'Connor", "Skid Row", "Smokey Robinson & The Miracles", "Stevie Ray Vaughan And Double Trouble", "The Cocteau Twins", "The Scorpions", "The Shirelles", "The Slits", "The Soup Dragons", "The Wrens", "X Ray Spex", "Boston", "Deep Purple", "Eddie Floyd", "Eva Cassidy", "Faith No More", "Focus", "Furniture", "Generation X", "Lauryn Hill", "Little Richard", "Maroon 5", "Merton Parkas", "My Red Cell", "Nat Adderly", "Pete Williams", "Radiohead", "Reef", "Rush", "Scott McKenzie", "Sebadoh", "Sum 41", "Suzanne Vega", "The Bees", "The Cult", "The Datsuns", "The Jam", "The Plastic Ono Band", "The Smashing Pumpkins", "The Style Council", "The Sugarcubes", "The Undertones", "Travis", "Yazoo", "Another Story", "Arctic Monkeys", "Blink 182", "Canned Heat", "Classix Nouveau", "Cream", "Dodgy", "Freda Payne", "Half Man Half Biscuit", "Hell Is For Heroes", "Jackson Browne", "John Renbourn", "Josh White", "Kate Bush", "Marillion", "Maximo Park", "Ministry", "Norma Waterson", "Pet Shop Boys", "Prodigy", "Razorlight", "Secret Affair", "Sleeper", "The Lambrettas", "The Lemonheads", "The Music", "The Pretty Things", "The Zombies", "Tim Buckley", "Tomorrow", "Westworld", "Yngwie Malmsteen", "Amadou & Mariam", "Blur", "Bonnie Tyler", "Booker T & The MGs", "Clearlake", "Craig Armstrong", "Danny Wilson", "Duane Eddy", "Four Tet", "Garnet Mimms", "George And Antony", "Gerry & The Pacemakers", "Jethro Tull", "Joe Strummer & The Mescaleros", "John Lennon", "Kate Rusby", "Lostprophets", "Love & Rockets", "Madonna", "MB2", "Morcheeba", "My Morning Jacket", "Oasis", "Peter Bruntnell", "Robert Wyatt", "Saint Etienne", "Serafin", "Sham 69", "Shed Seven", "Spirit", "Stellastarr*", "Sting", "Sufjan Stevens", "The Cure", "The Delays", "The Dramatics", "The Ramones", "The Yardbirds", "Tracy Chapman", "War of the Worlds", "Broken English", "Catatonia", "Chris Bell", "Dave Van Ronk", "Doc Watson & Clarence Ashley", "Elliott Smith", "Gorillaz", "Joe Cocker", "Johnny Cash", "Julian Cope", "Kraftwerk", "Martin Grech", "Muse", "No Doubt", "Paul Weller", "Pop Will Eat Itself", "Rage Against the Machine", "Renaissance", "Simon Dupree & The Big Sound", "Squire", "Terence Trent Darby", "The Darkness", "The Mighty Hannibal", "The Police", "The Red Hot Chili Peppers", "The Stranglers", "T'Pau", "Ultravox", "B.R.M.C.", "Clannad", "Crowded House", "Culture", "Duran Duran", "Echo & the Bunnymen", "Extreme", "Feeder", "Frijid Pink", "Genesis", "Green Day", "Hope", "James", "Jeff Buckley", "k.d. Lang", "Lambchop", "Manfred Mann", "Paul Young", "Polarized", "Status Quo", "The Blueskins", "The Misunderstood", "The Nashville Teens", "The Stone Roses", "Thirteen Senses", "Van Morrison", "Weezer", "999", "Arthur Conley", "Biffy Clyro", "Chris Spedding", "Chuck Berry", "Daniel Bedingfield", "Dexy's Midnight Runners", "Doves", "Gang Of Four", "Grand Drive", "Harry The Hipster Gibson", "Janis Ian", "Keane", "Killing Joke", "Lemon Jelly", "Madness", "Mercury Rev", "Ocean Colour Scene", "Pixies", "Remma", "Sigur Ros", "Spandau Ballet", "Steppenwolf", "Supergrass", "The Damned", "The Ruts", "The White Stripes", "Therapy", "Wilson Pickett", "Billie Holiday", "Charlotte Hatherley", "Chikinki", "Curve", "Dido", "Fontella Bass", "Future Kings Of Spain", "Giorgio Moroder & Phil Oakey", "Godsmack", "Japan", "Junior Walker & The All Stars", "Kingdom Come", "Nilsson", "Rainbow", "Rammstein", "Red Lorry Yellow Lorry", "REO Speedwagon", "Richard Hell And The Voidoids", "Santo & Johnny", "Spin Doctors", "Sub Circus", "SUB POP SUNDAY", "Tears For Fears", "The Creatures", "The Delgados", "The La's", "The Offspring", "The Temptations", "The Wedding Present", "The Who", "Willy Mason", "ABBA", "Aztec Camera", "Belinda Carlisle", "Brian Ferry", "David Axelrod", "Elbow", "Fields Of The Nephilim", "Heaven 17", "I Am Kloot", "Max Tundra", "Meat Loaf", "Neil Finn", "Nick Drake", "Oliver", "Public Image Ltd", "Silver Jews", "Sons And Daughters", "Soundgarden", "Stephen Tin Tin Duffy", "Stereo MC's", "Super Furry Animals", "Supertramp", "The Bonzo Dog Doo Dah Band", "The Cooper Temple Clause", "The Detroit Spinners", "The Libertines", "The Streets", "The The", "The Velvelettes", "The Velvet Underground", "Thin Lizzy", "Babyshambles", "Bachman Turner Overdrive", "Bonnie Dobson", "Bow Wow Wow", "Carter USM", "Cast", "Curtis Mayfield", "Foreigner", "Franz Ferdinand", "Grant Lee Phillips", "Jimmy Somerville", "Josh Rouse", "Magnum", "Nick Cave & the Bad Seeds", "Others", "Peter Sarstedt", "Showstoppers", "The Breeders", "The Distillers", "The Lovin' Spoonful", "The Marvelettes", "The Members", "The Ordinary Boys", "The Skids", "The Sweet", "Tony McRae", "Wire", "Yello", "Big Star", "Chris Farlowe", "Damien Rice", "Dick Gaughan", "Dobie Gray", "Emmanuel Jal", "Gary Numan", "Gun", "Howlin' Wolf", "Kirsty MacColl", "Long Pigs", "Sharon Shannon", "Siouxsie And The Banshees", "The Beloved", "The Cardigans", "The Icicle Works", "The Only Ones", "The Von Bondies", "Tom Robinson Band", "22-20's", "All About Eve", "Bessie Smith", "Boomtown Rats", "Chelsea", "Don MacLean", "Gene", "George Michael", "Jane's Addiction", "Joan Baez", "John Fred & His Playboy Band", "KT Tunstall", "Ladytron", "Love", "Montrose", "Mylo", "Pink Floyd", "Pulp", "Roy Orbison", "Ryan Adams", "Shamen", "Starsailor", "The Eagles", "The Everly Brothers", "The Hollies", "The Killers", "The Redskins", "The Vines", "Toto", "Tubeway Army", "Wondermints", "Air", "Ash", "Davey Graham", "Dream Academy", "Embrace", "Fleetwood Mac", "Human League", "Jefferson Airplane", "Jesus Jones", "Jimi Hendrix", "John Martyn", "Kula Shaker", "Leonard Cohen", "Moby", "Muddy Waters", "No Me Pises Que Llevo Chanclas", "PP Arnold", "Simon & Garfunkel", "Soft Cell", "The Animals", "The Beach Boys", "The Dandy Warhols", "The Departure", "The Fall", "The Isley Brothers", "The Rasmus", "The Supremes", "The Verve", "The Wannadies", "Unknown Artist", "Unpoc", "Adam & The Ants", "Amy Winehouse", "Björk", "Buzzcocks", "Dave Edmunds", "Evan Dando", "Ian Dury & The Blockheads", "Jackie Leven", "John Fahey", "Lead Belly", "Linton Kwesi Johnson", "Lloyd Cole And The Commotions", "Loudon Wainwright III", "Martha Reeves & The Vandellas", "Nazareth", "Pavement", "R.E.M.", "Rachel Mari Kimber", "Ram Jam", "Ron Sexsmith", "Sparks", "Subways", "Talk Talk", "The Birthday Party", "The Bluebells", "The Doors", "The Dubliners", "The Finn Brothers", "The Incredible String Band", "The Move", "The Pretenders", "The Smiths", "Willie Nelson", "Zero 7", "Alien Sex Fiend", "Beats International", "Bebel Gilberto", "Chicago", "David Bowie", "Dillinger", "Gary Moore", "Glen Campbell", "Iggy Pop", "Moloko", "Motorhead", "Penetration", "Primal Scream", "Queens Of The Stone Age", "Robert Plant", "Scritti Politti", "Sex Pistols", "The Coral", "The Flying Lizards", "The Jesus & Mary Chain", "The Mash", "The Teardrop Explodes", "Uriah Heep", "Visage", "Ambershades", "Archie Bronson Outfit", "Elvis Costello", "Fiction Factory", "Gary Jules", "Hood", "Lonnie Donegan", "Malvina Reynolds", "Massive Attack", "Me", "MEW", "Nina Simone", "Pat Benatar", "Psychedelic Furs", "Spear Of Destiny", "The Small Faces", "Violent Femmes", "Belouis Some", "Bryan Adams", "Captain Beefheart", "Chet Baker", "China Crisis", "Future Sound of London", "Hal", "Jilted John", "Kings of Leon", "Longview", "Mansun", "Nickleback", "Orbital", "Otis Redding", "Prefab Sprout", "Simple Minds", "Sisters Of Mercy", "T Rex", "The Boo Radleys", "The Kills", "The Passions", "The Turtles", "UB40", "Art Of Noise", "Blondie", "Damien Dempsey", "Donovan", "Electric Soft Parade", "Flying Burrito Bros", "Four Tops", "Frankie Goes To Hollywood", "Interpol", "Jonathan Richmna & The Modern Lovers", "Julie London", "Lou Reed", "Men At Work", "Neil Hannon & Yann Tiersen", "Nena", "Ozzy Osbourne", "Rilo Kiley", "Shirley Collins", "Stephen Fretwell", "The Beatles", "The Bravery", "The Chords", "The Flowerpot Men", "The Go! Team", "XTC", "Aqualung", "Belle & Sebastian", "Beth Gibbons & Rustin Man", "Big Brother & The Holding Company", "Blue Oyster Cult", "Cornershop", "Fun Boy Three", "Gene Loves Jezebel", "John Cooper Clarke", "Kaiser Chiefs", "Kim Wilde", "Live", "New York Dolls", "Placebo", "Propaganda", "Richie Havens", "Robert Johnson", "Rory McLeod", "Snow Patrol", "Stephen Malkmus & The Jicks", "The Charlatans", "The Monochrome Set", "The Shadows", "The Swinging Blue Jeans", "The Zutons" }