Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. void print(int a[],int n){
  6. int i;
  7. for(i=0;i<n;i++)
  8. printf("%d ",a[i]);
  9. printf("\n");
  10. }
  11. void insertionSort(int a[],int n){
  12. int i,j,t;
  13. for(i=0;i<n;i++){
  14. t=a[i];
  15. for(j=n;j>0&&a[j-1]>t;j--)
  16. a[j]=a[j-1];
  17. a[j]=t;
  18. }
  19. }
  20. void selectionSort(int a[],int n){
  21. int i,j,min,t;
  22. for(i=0;i<n-1;i++){
  23. min=i;
  24. for(j=i+1;j<n;j++){
  25. if(a[j]>a[min])min=j;
  26. }
  27. t=a[i];
  28. a[i]=a[min];
  29. a[min]=t;
  30. }
  31. }
  32. void bubbleSort(int a[],int n){
  33. int i,j,t;
  34. for(i=0;i<n;i++)
  35. for(j=i+1;j<n;j++)
  36. if(a[i]>a[j]){
  37. t=a[i];
  38. a[i]=a[j];
  39. a[j]=t;
  40. }
  41. }
  42. void shellSort(int a[],int n){
  43. int i,j,k,t;
  44. for(i=n/2;i>0;i/=2)
  45. for(j=i;j<n;j++)
  46. for(k=j-i;k>=0;k=k-i)
  47. if(a[k+i]<a[k]){
  48. t=a[k+i];
  49. a[k+i]=a[k];
  50. a[k]=t;
  51. }
  52. }
  53.  
  54. /*void mergeSort(){}
  55. void heapSort(){}
  56. void quickSort(int a[],int n){}
  57. void quick3Sort(int a[],int n){}*/
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. int main(){
  74. char ime[20];
  75. int i,n,b,a[20],x=1;
  76. srand(time(NULL));
  77. b=rand()%100;
  78. printf("Unesite svoje ime: ");
  79. scanf("%s",ime);
  80. n=strlen(ime);
  81. printf("Pokusajte pogoditi broj izmedju 0 i 100 imate %d pokusaja!\n",n);
  82. for(i=0;i<n;i++){
  83. scanf("%d",&a[i]);
  84. if(a[i]<b)printf("Slucajni broj je veci od unesenog!\n");
  85. if(a[i]>b)printf("Slucajni broj je manji od unesenog!\n");
  86. if(a[i]==b){printf("Svaka cast!Pogodili ste slucajan broj!\n");n=++i;x=0;break;}
  87. }
  88. if(x)printf("Tacan broj je %d\n",b);
  89. printf("Brojevi koje ste redom unosili su:\n");
  90. print(a,n);
  91. printf("\nRastuci BubbleSort:\n");
  92. bubbleSort(a,n);
  93. print(a,n);
  94. printf("\nOpadjuci SelectionSort:\n");
  95. selectionSort(a,n);
  96. print(a,n);
  97. system("pause");
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement