In this post search, insert and delete operation in an unsorted array is discussed.

 

[ad type=”banner”]

Search Operation

In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element.

 

[ad type=”banner”]

C Programming

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20to%20implement%20linear%20search%20in%20%0A%2F%2F%20unsorted%20array%0A%23include%3Cstdio.h%3E%0A%20%0A%2F*%20Function%20to%20implement%20search%20operation%20*%2F%0Aint%20findElement(int%20arr%5B%5D%2C%20int%20n%2Cint%20key)%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(arr%5Bi%5D%20%3D%3D%20key)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20i%3B%0A%20%0A%20%20%20%20return%20-1%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B12%2C%2034%2C%2010%2C%206%2C%2040%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%0A%20%20%20%20%2F%2FUsing%20a%20last%20element%20as%20search%20element%0A%20%20%20%20int%20key%20%3D40%3B%0A%20%20%20%20int%20position%20%3D%20findElement(arr%2Cn%2Ckey)%3B%0A%20%0A%20%20%20%20if%20(position%3D%3D-1)%0A%20%20%20%20%20%20%20%20printf(%22Element%20not%20found%22)%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20printf(%22Element%20Found%20at%20Position%3A%20%25d%22%2C%20position%2B1%20)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A” message=”C” highlight=”” provider=”manual”/]

 

[ad type=”banner”]

Output:

Element Found at Position: 5

 

[ad type=”banner”]

Insert Operation

In an unsorted array, the insert operation is faster as compared to sorted array because we don’t have to care about the position at which the element is to be placed.

 

[ad type=”banner”]

C Programming

 

[ad type=”banner”]

 

Output:

Before Insertion: 12 16 20 40 50 70 
After Insertion: 12 16 20 40 50 70 26 

 

[ad type=”banner”]

Delete Operation

In delete operation, the element to be deleted is searched using the linear search and then delete operation is performed followed by shifting the elements.

 

[ad type=”banner”]

C Programming

[pastacode lang=”c” manual=”%0A%2F%2F%20C%20program%20to%20implement%20delete%20operation%20in%20a%0A%2F%2F%20unsorted%20array%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20To%20search%20a%20key%20to%20be%20deleted%0Aint%20findElement(int%20arr%5B%5D%2C%20int%20n%2C%20int%20key)%3B%0A%20%0A%2F*%20Function%20to%20delete%20an%20element%20*%2F%0Aint%20deleteElement(int%20arr%5B%5D%2C%20int%20n%2C%20int%20key)%0A%7B%0A%20%20%20%20%2F%2F%20Find%20position%20of%20element%20to%20be%20deleted%0A%20%20%20%20int%20pos%20%3D%20findElement(arr%2C%20n%2C%20key)%3B%0A%20%0A%20%20%20%20if%20(pos%3D%3D-1)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22Element%20not%20found%22)%3B%0A%20%20%20%20%20%20%20%20return%20n%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Deleting%20element%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%3Dpos%3B%20i%3Cn-1%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20arr%5Bi%2B1%5D%3B%0A%20%0A%20%20%20%20return%20n-1%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20implement%20search%20operation%20*%2F%0Aint%20findElement(int%20arr%5B%5D%2C%20int%20n%2C%20int%20key)%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(arr%5Bi%5D%20%3D%3D%20key)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20i%3B%0A%20%0A%20%20%20%20return%20-1%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20code%0Aint%20main()%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B10%2C%2050%2C%2030%2C%2040%2C%2020%7D%3B%0A%20%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20int%20key%20%3D%2030%3B%0A%20%0A%20%20%20%20printf(%22Array%20before%20deletion%5Cn%22)%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20printf(%22%25d%20%20%22%2C%20arr%5Bi%5D)%3B%0A%20%0A%20%20%20%20n%20%3D%20deleteElement(arr%2C%20n%2C%20key)%3B%0A%20%0A%20%20%20%20printf(%22%5Cn%5CnArray%20after%20deletion%5Cn%22)%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20printf(%22%25d%20%20%22%2C%20arr%5Bi%5D)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A%20%0A” message=”C” highlight=”” provider=”manual”/]

 

[ad type=”banner”]

Output:

Array before deletion
10 50 30 40 20 

Array after deletion
10 50 40 20 

 Time complexities:
Search: O(n)
Insert: O(1)
Delete: O(n)

 

Categorized in: