{"id":26677,"date":"2017-09-06T00:22:40","date_gmt":"2017-09-05T18:52:40","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26677"},"modified":"2017-09-06T00:22:40","modified_gmt":"2017-09-05T18:52:40","slug":"search-insert-delete-unsorted-array","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/search-insert-delete-unsorted-array\/","title":{"rendered":"C Programming-Search, insert and delete in an unsorted array"},"content":{"rendered":"<p>In this post search, insert and delete operation in an unsorted array is discussed.<\/p>\n<p>\u00a0<\/p>\n[ad type=\u201dbanner\u201d]\n<p><b>Search Operation<\/b><\/p>\n<p>In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element.<\/p>\n<p>\u00a0<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>C Programming<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%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\u201d message=\u201dC\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>\u00a0<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Output:<\/strong><\/p>\n<pre>Element Found at Position: 5\r\n<\/pre>\n<p>\u00a0<\/p>\n[ad type=\u201dbanner\u201d]\n<p align=\"center\"><strong>Insert Operation<\/strong><\/p>\n<p>In an unsorted array, the insert operation is faster as compared to sorted array because we don\u2019t have to care about the position at which the element is to be placed.<\/p>\n<p>\u00a0<\/p>\n[ad type=\u201dbanner\u201d]\n<p><b>C Programming<\/b><\/p>\n<p>\u00a0<\/p>\n[ad type=\u201dbanner\u201d]\n<p>\u00a0<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>Before Insertion: 12 16 20 40 50 70 \r\nAfter Insertion: 12 16 20 40 50 70 26 \r\n<\/pre>\n<p>\u00a0<\/p>\n[ad type=\u201dbanner\u201d]\n<p align=\"center\"><strong>Delete Operation<\/strong><\/p>\n<p>In delete operation, the element to be deleted is searched using the <a href=\"http:\/\/quiz.geeksforgeeks.org\/linear-search\/\">linear search<\/a> and then delete operation is performed followed by shifting the elements.<\/p>\n<p>\u00a0<\/p>\n[ad type=\u201dbanner\u201d]\n<p><b>C Programming<\/b><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%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\u201d message=\u201dC\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>\u00a0<\/p>\n[ad type=\u201dbanner\u201d]\n<p>Output:<\/p>\n<pre>Array before deletion\r\n10 50 30 40 20 \r\n\r\nArray after deletion\r\n10 50 40 20 \r\n<\/pre>\n<p><strong>\u00a0Time complexities:<\/strong><br \/>\n<strong>Search:<\/strong> O(n)<br \/>\n<strong>Insert:<\/strong> O(1)<br \/>\n<strong>Delete: <\/strong>O(n)<\/p>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C Programming-Search, insert and delete in an unsorted array-In this post search, insert and delete operation in an unsorted array is discussed.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79658],"tags":[],"class_list":["post-26677","post","type-post","status-publish","format-standard","hentry","category-array"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26677"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26677\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}