File tree Expand file tree Collapse file tree 10 files changed +393
-0
lines changed Expand file tree Collapse file tree 10 files changed +393
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <time.h>
3
+ int search (char A [],char B [],int n ,int m )
4
+ {
5
+ int i ,j ,pos = -1 ;
6
+ for (i = 0 ;i < n - m ;i ++ )
7
+ {
8
+ if (A [i ]== B [0 ])
9
+ {
10
+ j = 0 ;
11
+ while (j < m && A [i + j ]== B [j ])
12
+ j = j + 1 ;
13
+ }
14
+ if (j == m )
15
+ pos = i ;
16
+ }
17
+ return pos ;
18
+ }
19
+ int main ()
20
+ {
21
+ int n ,m ,pos ;
22
+ clock_t start ,end ;
23
+ double t ;
24
+ printf ("Enter the respective sizes of Text and Pattern arrays such that text has more size\n" );
25
+ scanf ("%d%d" ,& n ,& m );
26
+ if (n >=m )
27
+ {
28
+ char TEXT [n ],PATTERN [m ];
29
+ printf ("Enter the text string\n" );
30
+ scanf ("%s" ,TEXT );
31
+ printf ("Enter the pattern string\n" );
32
+ scanf ("%s" ,PATTERN );
33
+ start = clock ();
34
+ pos = search (TEXT ,PATTERN ,n ,m );
35
+ end = clock ();
36
+ t = (double )(start - end )/CLOCKS_PER_SEC ;
37
+ printf ("The element is found at %d index\n" ,pos );
38
+ printf ("The time taken is %f\n" ,t );
39
+ }
40
+ else
41
+ {
42
+ printf ("The size of pattern cannot be more than text!!" );
43
+ }
44
+ return 0 ;
45
+ }
Original file line number Diff line number Diff line change
1
+ #include < stdio.h>
2
+ #include < time.h>
3
+ void bubble_sort (int A[],int n)
4
+ {
5
+ int i,j,temp;
6
+ for (i=0 ;i<n-1 ;i++)
7
+ {
8
+ for (j=0 ;j<n-i-1 ;j++)
9
+ {
10
+ if (A[j]>A[j+1 ])
11
+ {
12
+ temp = A[j+1 ];
13
+ A[j+1 ] = A[j];
14
+ A[j] = temp;
15
+ }
16
+ }
17
+ }
18
+ }
19
+ void selection_sort (int A[],int n)
20
+ {
21
+ int i,j,temp;
22
+ for (i=0 ;i<n-1 ;i++)
23
+ {
24
+ for (j=i+1 ;j<n;j++)
25
+ {
26
+ if (A[j]<A[i])
27
+ {
28
+ temp = A[i];
29
+ A[i] = A[j];
30
+ A[j] = temp;
31
+ }
32
+ }
33
+ }
34
+ }
35
+ int main ()
36
+ {
37
+ int n,i;
38
+ clock_t st,en;
39
+ double t;
40
+ printf (" Enter size of the array: " );
41
+ scanf (" %d" ,&n);
42
+ int A[n];
43
+ for (i=0 ;i<n;i++)
44
+ {
45
+ printf (" Enter the %d element of the array: " ,i+1 );
46
+ scanf (" %d" ,&A[i]);
47
+ }
48
+ int B[n];
49
+ for (i=0 ;i<n;i++)
50
+ B[i]=A[i];
51
+ st = clock ();
52
+ bubble_sort (A,n);
53
+ en= clock ();
54
+ t= double (en-st)/CLOCKS_PER_SEC;
55
+ printf (" Time taken for Bubble sort is :%f \n " ,t);
56
+ st = clock ();
57
+ selection_sort (B,n);
58
+ en=clock ();
59
+ t= (double )(en-st)/CLOCKS_PER_SEC;
60
+ printf (" Time taken for Selection sort is :%f \n " ,t);
61
+ return 0 ;
62
+ }
Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <time.h>
3
+ int linearsearch (int A [],int el ,int n )
4
+ {
5
+ int i ,flag = 0 ;
6
+ for (i = 0 ;i < n ;i ++ )
7
+ {
8
+ if (A [i ]== el )
9
+ {
10
+ flag = 1 ;
11
+ break ;
12
+ }
13
+ }
14
+ return flag ;
15
+ }
16
+ int main ()
17
+ {
18
+ int n ,i ,el ,flag ;
19
+ clock_t start ,end ;
20
+ double time ;
21
+ printf ("Enter the number of elements in the array: " );
22
+ scanf ("%d" ,& n );
23
+ int A [n ];
24
+ for (i = 0 ;i < n ;i ++ )
25
+ {
26
+ printf ("Enter the %d element of the array: " ,i + 1 );
27
+ scanf ("%d" ,& A [i ]);
28
+ }
29
+ printf ("Enter the element you want to search in the array: " );
30
+ scanf ("%d" ,& el );
31
+ start = clock ();
32
+ flag = linearsearch (A ,el ,n );
33
+ end = clock ();
34
+ time = (double )(start - end )/CLOCKS_PER_SEC ;
35
+ if (flag == 1 )
36
+ printf ("The element exists in the array" );
37
+ else
38
+ printf ("The element does not exist in the array\n" );
39
+ printf ("Time taken for linear search is :%f\n" ,time );
40
+ return 0 ;
41
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+ int main ()
4
+ {
5
+ int n,i,j,temp;
6
+ cout<<" Enter number of elements you want in array: " ;
7
+ cin>>n;
8
+ int A[n];
9
+ for (i=0 ;i<n;i++)
10
+ {
11
+ cout<<" Enter " <<i+1 <<" element of the array: " ;
12
+ cin>>A[i];
13
+ }
14
+ cout<<" Lets sort the array by Bubble Sorting" <<endl;
15
+ for (i=0 ;i<n-1 ;i++)
16
+ {
17
+ for (j=0 ;j<n-i-1 ;j++)
18
+ {
19
+ if (A[j]>A[j+1 ])
20
+ {
21
+ temp = A[j+1 ];
22
+ A[j+1 ] = A[j];
23
+ A[j] = temp;
24
+ }
25
+ }
26
+ }
27
+ cout<<" Lets see the sorted array" <<endl;
28
+ for (i=0 ;i<n;i++)
29
+ {
30
+ cout<<i+1 <<" element of the sorted array is: " <<A[i]<<endl;
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+ int main ()
4
+ {
5
+ int n,i,j,key;
6
+ cout<<" Enter size of array you want: " ;
7
+ cin>>n;
8
+ int A[n];
9
+ for (i=0 ;i<n;i++)
10
+ {
11
+ cout<<" Enter " <<i+1 <<" element of array" <<endl;
12
+ cin>>A[i];
13
+ }
14
+ cout<<" Lets do Insertion sort" <<endl;
15
+ for (i=0 ;i<n;i++)
16
+ {
17
+ key = A[i];
18
+ j=i-1 ;
19
+ while (j>=0 && A[j]>key)
20
+ {
21
+ A[j+1 ] = A[j];
22
+ j=j-1 ;
23
+ }
24
+ A[j+1 ]=key;
25
+ }
26
+ cout<<" Sorted Array is:" <<endl;
27
+ for (i=0 ;i<n;i++)
28
+ {
29
+ cout<<i+1 <<" Element is :" <<A[i]<<endl;
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+ // l is lowest index and r is the max index (left and right)
4
+ void merge (int A[],int l,int m ,int r)
5
+ {
6
+ // this function is to merge the partitioned arrays
7
+ // creating temp arrays
8
+ int n1 = m-l+1 ;
9
+ int n2 = r-m;
10
+ int A1[n1],A2[n2],i,j,k;
11
+ // First subarray begins from l to m
12
+ // second subarray begins from m+1 to r
13
+ // Filling the array
14
+ for (i=0 ;i<n1;i++)
15
+ {
16
+ A1[i] = A[l+i];
17
+ }
18
+ for (j=0 ;j<n2;j++)
19
+ {
20
+ A2[j] = A[m+1 +j];
21
+ }
22
+ // Refilling the old array with correct order
23
+ i=0 ; // counter for A1
24
+ j=0 ;// counter for A2
25
+ k=l;// counter for A
26
+ while ((i<n1) && (j<n2))
27
+ {
28
+ if (A1[i]<=A2[j])
29
+ {
30
+ A[k]=A1[i];
31
+ i++;
32
+ }
33
+ else
34
+ {
35
+ A[k]=A2[j];
36
+ j++;
37
+ }
38
+ k++;
39
+ }
40
+ // If any elements remain fill them because above while loop ends when either of the subarryas is filled
41
+ while (i<n1)
42
+ {
43
+ A[k]=A1[i];
44
+ i++;
45
+ k++;
46
+ }
47
+ while (j<n2)
48
+ {
49
+ A[k]=A2[j];
50
+ k++;
51
+ j++;
52
+ }
53
+ }
54
+ // /////////////////////////////////////////////////////////////////
55
+ // Main merge sort function
56
+ void mergesort (int A[],int l,int r)
57
+ {
58
+ if (l<r)
59
+ {
60
+ int m=(l+r)/2 ;
61
+ // Sorting to first and second halves
62
+ mergesort (A,l,m);
63
+ mergesort (A,m+1 ,r);
64
+ // merging them
65
+ merge (A,l,m,r);
66
+ }
67
+ }
68
+ int main ()
69
+ {
70
+ int n,l,r,i;
71
+ cout<<" Enter the array size you want: " ;
72
+ cin>>n;
73
+ int A[n];
74
+ for (i=0 ;i<n;i++)
75
+ {
76
+ cout<<" Enter " <<i+1 <<" element of the array: " ;
77
+ cin>>A[i];
78
+ }
79
+ cout<<" Lets do merge sorting" <<endl;
80
+ mergesort (A,0 ,n-1 );
81
+ for (i=0 ;i<n;i++)
82
+ {
83
+ cout<<i+1 <<" element of the array is: " <<A[i]<<endl;
84
+ }
85
+ }
Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <time.h>
3
+ ///////////////////////////
4
+ void swap (int * x ,int * y )
5
+ {
6
+ int temp ;
7
+ temp = * x ;
8
+ * x = * y ;
9
+ * y = temp ;
10
+ }
11
+ ////////////////////////////
12
+ int partition (int A [],int low ,int high )
13
+ {
14
+ int i ,j ,pivot ;
15
+ pivot = A [high ];
16
+ i = low - 1 ;
17
+ for (j = low ;j <=high - 1 ;j ++ )
18
+ {
19
+ if (A [j ]<=pivot )
20
+ {
21
+ i ++ ;
22
+ swap (& A [i ],& A [j ]);
23
+ }
24
+ }
25
+ swap (& A [i + 1 ],& A [high ]);
26
+ return (i + 1 );
27
+ }
28
+ //////////////////////////////
29
+ void quicksort (int A [],int low ,int high )
30
+ {
31
+ int pi ;
32
+ if (low < high )
33
+ {
34
+ pi = partition (A ,low ,high );
35
+ quicksort (A ,low ,pi - 1 );
36
+ quicksort (A ,pi + 1 ,high );
37
+ }
38
+ }
39
+ //////////////////////////
40
+ int main ()
41
+ {
42
+ clock_t start ,end ;
43
+ double time ;
44
+ int n ,i ;
45
+ printf ("Enter the number of elements in the array: " );
46
+ scanf ("%d" ,& n );
47
+ int A [n ];
48
+ for (i = 0 ;i < n ;i ++ )
49
+ {
50
+ printf ("Enter the %d element of the array: " ,i + 1 );
51
+ scanf ("%d" ,& A [i ]);
52
+ }
53
+ printf ("Lets do the quick sort of given array\n" );
54
+ start = clock ();
55
+ quicksort (A ,0 ,n - 1 );
56
+ end = clock ();
57
+ time = (double )((start - end )/CLOCKS_PER_SEC );
58
+ printf ("The sorted array is:\n" );
59
+ for (i = 0 ;i < n ;i ++ )
60
+ {
61
+ printf ("The %d element of the sorted array is: %d\n" ,i + 1 ,A [i ]);
62
+ }
63
+ printf ("Time taken for the quick sort is: %f\n" ,time );
64
+ return 0 ;
65
+ }
You can’t perform that action at this time.
0 commit comments