{"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>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\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>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>C Programming<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ C program to implement linear search in <br\/>\/\/ unsorted array<br\/>#include&lt;stdio.h&gt;<br\/> <br\/>\/* Function to implement search operation *\/<br\/>int findElement(int arr[], int n,int key)<br\/>{<br\/>    int i;<br\/>    for (i=0; i&lt;n; i++)<br\/>        if (arr[i] == key)<br\/>            return i;<br\/> <br\/>    return -1;<br\/>}<br\/> <br\/>\/* Driver program to test above function *\/<br\/>int main()<br\/>{<br\/>    int arr[] = {12, 34, 10, 6, 40};<br\/>    int n = sizeof(arr)\/sizeof(arr[0]);<br\/> <br\/>    \/\/Using a last element as search element<br\/>    int key =40;<br\/>    int position = findElement(arr,n,key);<br\/> <br\/>    if (position==-1)<br\/>        printf(&quot;Element not found&quot;);<br\/>    else<br\/>        printf(&quot;Element Found at Position: %d&quot;, position+1 );<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Output:<\/strong><\/p>\n<pre>Element Found at Position: 5\r\n<\/pre>\n<p>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\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>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><b>C Programming<\/b><\/p>\n<p>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>&nbsp;<\/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>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\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\/\" target=\"_blank\" rel=\"noopener\">linear search<\/a> and then delete operation is performed followed by shifting the elements.<\/p>\n<p>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><b>C Programming<\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\"><br\/>\/\/ C program to implement delete operation in a<br\/>\/\/ unsorted array<br\/>#include&lt;stdio.h&gt;<br\/> <br\/>\/\/ To search a key to be deleted<br\/>int findElement(int arr[], int n, int key);<br\/> <br\/>\/* Function to delete an element *\/<br\/>int deleteElement(int arr[], int n, int key)<br\/>{<br\/>    \/\/ Find position of element to be deleted<br\/>    int pos = findElement(arr, n, key);<br\/> <br\/>    if (pos==-1)<br\/>    {<br\/>        printf(&quot;Element not found&quot;);<br\/>        return n;<br\/>    }<br\/> <br\/>    \/\/ Deleting element<br\/>    int i;<br\/>    for (i=pos; i&lt;n-1; i++)<br\/>        arr[i] = arr[i+1];<br\/> <br\/>    return n-1;<br\/>}<br\/> <br\/>\/* Function to implement search operation *\/<br\/>int findElement(int arr[], int n, int key)<br\/>{<br\/>    int i;<br\/>    for (i=0; i&lt;n; i++)<br\/>        if (arr[i] == key)<br\/>            return i;<br\/> <br\/>    return -1;<br\/>}<br\/> <br\/>\/\/ Driver code<br\/>int main()<br\/>{<br\/>    int i;<br\/>    int arr[] = {10, 50, 30, 40, 20};<br\/> <br\/>    int n = sizeof(arr)\/sizeof(arr[0]);<br\/>    int key = 30;<br\/> <br\/>    printf(&quot;Array before deletion\\n&quot;);<br\/>    for (i=0; i&lt;n; i++)<br\/>      printf(&quot;%d  &quot;, arr[i]);<br\/> <br\/>    n = deleteElement(arr, n, key);<br\/> <br\/>    printf(&quot;\\n\\nArray after deletion\\n&quot;);<br\/>    for (i=0; i&lt;n; i++)<br\/>      printf(&quot;%d  &quot;, arr[i]);<br\/> <br\/>    return 0;<br\/>}<br\/> <\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\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>&nbsp;<\/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}]}}