File tree 2 files changed +74
-54
lines changed
2 files changed +74
-54
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ struct Array
5
+ {
6
+ int * A;
7
+ int size;
8
+ int length;
9
+ };
10
+
11
+ void Display (struct Array * arr)
12
+ {
13
+ int i;
14
+ cout << " The elements of the array is " << endl;
15
+ for (i = 0 ; i < arr->length ; i++)
16
+ cout << arr->A [i] << " " ;
17
+ }
18
+ int Findmax (struct Array * arr) // findinf the max of the array
19
+ {
20
+ int a = INT_MIN;
21
+ int i;
22
+ for (i = 0 ; i < arr->length ; i++)
23
+ {
24
+ if (arr->A [i] > a)
25
+ a = arr->A [i];
26
+ }
27
+ return a;
28
+ }
29
+
30
+ void Countsort (struct Array * arr)
31
+ {
32
+ int max, i;
33
+ int * C;
34
+ max = Findmax (arr); // finding the max element of the array
35
+ C = new int [max + 1 ]; // create an array of max size element in the array
36
+ for (i = 0 ; i < max + 1 ; i++)
37
+ C[i] = 0 ; // initialize all the count array with zero
38
+ for (i = 0 ; i < arr->length ; i++)
39
+ {
40
+ C[arr->A [i]]++; // count the number of element present in the array and increament count
41
+ }
42
+ int j;
43
+ i = 0 , j = 0 ;
44
+ while (i < max + 1 )// with this loop depending on the count of the elements will be filled in the array in sorted manner
45
+ {
46
+ if (C[i] > 0 ) {
47
+ arr->A [j++] = i;
48
+ C[i]--;
49
+ }
50
+ else
51
+ i++;
52
+
53
+ }
54
+
55
+ }
56
+ int main ()
57
+ {
58
+ struct Array arr;
59
+ cout << " Enter the size of the Array" << endl;
60
+ cin >> arr.size ;
61
+ arr.A = new int [arr.size ];
62
+ int no, i;
63
+ cout << " Enter the length of the Array " << endl;
64
+ cin >> no;
65
+ arr.length = 0 ;
66
+ cout << " Enter the elements of the Array" << endl;
67
+ for (i = 0 ; i < no; i++)
68
+ cin >> arr.A [i];
69
+ arr.length = no;
70
+ Countsort (&arr);
71
+ // cout<<"The max of the array is -> "<<Findmax(&arr);
72
+ Display (&arr);
73
+ return 0 ;
74
+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments