bipo143

bubble sort ,linearsearch 18.02.17

Feb 18th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. void Display_Array(int b[],int k);
  2. void Edit(int b[],int pp,int vv);
  3. int Insert_LastPosition(int b[],int nn,int vv);
  4. int Insertion_SpecificPosition(int b[],int nn,int vv,int pp);
  5. int Deletion(int b[],int nn,int pp);
  6. void Linear_Search(int b[], int nn, int vv);
  7. void Bubble_Sort(int b[], int nn);
  8. int main()
  9. {
  10. int a[100],n,i,c,b=1,v,p;
  11. printf("\n Enter how many value\n");
  12. scanf("%d",&n);
  13. printf("\n Enter value\n");
  14. for(i=1;i<=n;i++)
  15. scanf("%d",&a[i]);
  16. while(b)
  17. {
  18. printf("\n-----Menu----\n");
  19. printf("\n Press 0 for Quite\n");
  20. printf("\n Press 1 for Display\n");
  21. printf("\n Press 2 for Insertion at last position\n");
  22. printf("\n Press 3 for Insertion at specific position\n");
  23. printf("\n Press 4 for Delete from specific position\n");
  24. printf("\n Press 5 for Linear search\n");
  25. printf("\n Press 6 for Bubble Sort \n");
  26. printf("\n Press 7 for Binary Search\n");
  27. printf("\n Press 8 for Edit\n");
  28. printf("\n Enter your choice\n");
  29. scanf("%d",&c);
  30.  
  31. switch(c)
  32. {
  33.  
  34. case 0: b=0;
  35.  
  36. break;
  37.  
  38. case 1: printf("\n choice Display\n");
  39.  
  40. if(n!=0)
  41.  
  42. Display_Array(a,n);
  43. else
  44. printf("\n No data to display\n");
  45.  
  46. break;
  47.  
  48. case 8: printf("\n choice=Edit\n");
  49.  
  50. if(n!=0)
  51. {
  52. printf("\n Enter new value\n");
  53. scanf("%d",&v);
  54. M: printf("Enter position between %d to %d\n",1,n);
  55. scanf("%d",&p);
  56. if (p>=1 && p<=n)
  57.  
  58. Edit (a,p,v);
  59.  
  60. else
  61. {
  62. printf("\n Wrong Position\n");
  63. goto M;
  64. }
  65. }
  66. else
  67. printf("No data to Edit\n");
  68.  
  69. break;
  70. case 2:printf("\n Choice=Insertion at last position\n");
  71. printf("\n Enter new values\n");
  72. scanf("%d",&v);
  73. n=Insert_LastPosition(a,n,v);
  74. break;
  75.  
  76.  
  77. case 3:printf("\n Choice=Insertion at specification\n");
  78.  
  79. if(n!=0)
  80. {
  81. k:printf("\n Enter position between %d to %d\n",1,n+1);
  82. scanf("%d", &p);
  83. if(p>=1 && p<=n+1)
  84. {
  85. printf("\n Enter new value \n");
  86. scanf("%d",&v);
  87. n=Insertion_SpecificPosition(a,n,v,p);
  88. printf("\n\n Inserted at position %d\n",p);
  89. }
  90. else
  91. {
  92. printf("\n Wrong position\n");
  93. goto k;
  94. }
  95. }
  96.  
  97. else
  98. printf("\n The array is empty\n");
  99.  
  100.  
  101. case 4:printf("\n Choice=Delete from Array\n");
  102.  
  103. if(n!=0)
  104. {
  105. z:printf("\nEnter Position between %d to %d",1,n);
  106. scanf("%d",&p);
  107. if(p>=1 && p<=n)
  108. {
  109. printf("\n Deleted value= %d\n",a[p]);
  110. n=Deletion(a,n,p);
  111. printf("\n Deleted sucessfully from position %d\n",p);
  112. }
  113.  
  114. else
  115. {
  116. printf("\n Wrong position\n");
  117. goto z;
  118. }
  119. }
  120.  
  121. else
  122. {
  123.  
  124. printf("\n No data to Delete\n");
  125.  
  126. break;
  127.  
  128. case 5:
  129.  
  130. printf("\n choice=Linear_Search\n");
  131. if(n!=0)
  132. {
  133. printf("\n Enter value for searching\n");
  134. scanf("%d",&v);
  135. Linear_Search(a,n,v);
  136.  
  137. }
  138. else
  139. printf("\n No Data To searching\n");
  140. break;
  141. }
  142.  
  143.  
  144.  
  145. default: printf("\n wrong choice\n");
  146. break;
  147.  
  148. case 6:
  149.  
  150. printf("\n choice=Bubble_Sort\n");
  151. if(n!=0)
  152. Bubble_Sort(a,n);
  153. else
  154. printf("\n The Array is empty\n");
  155.  
  156. break;
  157.  
  158.  
  159. }
  160. }
  161. }
  162.  
  163. void Display_Array(int b[],int k)
  164. {
  165.  
  166. int j;
  167. printf("\n The values are:\n");
  168. for(j=1;j<=k;j++)
  169. printf("%d\n",b[j]);
  170. }
  171. void Edit (int b[],int pp,int vv)
  172. {
  173. b[pp]=vv;
  174. printf("\n Edited Succesively\n");
  175. }
  176. int Insert_LastPosition (int b[],int nn,int vv)
  177. {
  178. b[nn+1]=vv;
  179. printf("\n Inserted successfully \n");
  180. return nn+1;
  181. }
  182.  
  183. int Insertion_SpecificPosition(int b[],int nn,int vv,int pp)
  184. {
  185. int j;
  186. for(j=nn;j>=pp;j--)
  187. b[j+1]=b[j];
  188.  
  189. b[pp]=vv;
  190.  
  191. return nn+1;
  192.  
  193. }
  194. int Deletion(int b[],int nn,int pp)
  195. {
  196. int j;
  197. for(j=pp;j<nn;j++)
  198. b[j]=b[j+1];
  199. return nn-1;
  200. }
  201.  
  202. void Linear_Search(int b[], int nn, int vv)
  203. {
  204. int j, c=0;
  205. for (j=1; j<=nn; j++)
  206. {
  207. if (b[j]==vv)
  208. {
  209. printf("\n Found at Position=%d",j);
  210. c=c+1;
  211. }
  212. }
  213. if(c==0)
  214. printf("\n Not Found");
  215. else
  216. printf("\n Total Found=%d\n ",c);
  217.  
  218. }
  219. void Bubble_Sort(int b[], int nn)
  220. {
  221. int i,j,t;
  222. for(i=1; i<=nn; i++)
  223.  
  224. {
  225. for(j=1;j<nn-i;j++)
  226. {
  227. if(b[j]>b[j+1])
  228. {
  229. t=b[j];
  230. b[j]=b[j+1];
  231. b[j+1]=t;
  232.  
  233. }
  234.  
  235.  
  236. }
  237.  
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment