Skip to content

Commit 25fb8ed

Browse files
committed
added counting sort code
1 parent 84e9185 commit 25fb8ed

File tree

2 files changed

+307
-0
lines changed

2 files changed

+307
-0
lines changed

DoublyLinkedlList.cpp

+266
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
// structure for doubly linked list
4+
struct node
5+
{
6+
int data;
7+
struct node *next;
8+
struct node *prev;
9+
};
10+
// global variables
11+
struct node *head = NULL;
12+
struct node *tail = NULL;
13+
int size = 0;
14+
// function for inserting a node at beginning
15+
void InsertAtBegin(int data)
16+
{
17+
struct node *newNode = (struct node *)malloc(sizeof(struct node));
18+
newNode->data = data;
19+
newNode->next = NULL;
20+
newNode->prev = NULL;
21+
if (head == NULL)
22+
{
23+
head = newNode;
24+
tail = newNode;
25+
}
26+
else
27+
{
28+
newNode->next = head;
29+
head->prev = newNode;
30+
head = newNode;
31+
}
32+
size++;
33+
}
34+
// function for inserting a node at end
35+
void InsertAtEnd(int data)
36+
{
37+
struct node *newNode = (struct node *)malloc(sizeof(struct node));
38+
newNode->data = data;
39+
newNode->next = NULL;
40+
newNode->prev = NULL;
41+
if (head == NULL)
42+
{
43+
head = newNode;
44+
tail = newNode;
45+
}
46+
else
47+
{
48+
newNode->prev = tail;
49+
tail->next = newNode;
50+
tail = newNode;
51+
}
52+
size++;
53+
}
54+
// function for inserting a node at position
55+
void InsertAtPosition(int data, int position)
56+
{
57+
if (position >= 0 && position <= size)
58+
{
59+
if (position == 0)
60+
{
61+
InsertAtBegin(data);
62+
}
63+
else if (position == size)
64+
{
65+
InsertAtEnd(data);
66+
}
67+
else
68+
{
69+
struct node *newNode = (struct node *)malloc(sizeof(struct
70+
node));
71+
newNode->data = data;
72+
struct node *ptr = head;
73+
int i = 0;
74+
while (i < position)
75+
{
76+
ptr = ptr->next;
77+
i++;
78+
}
79+
newNode->next = ptr;
80+
newNode->prev = ptr->prev;
81+
ptr->prev->next = newNode;
82+
ptr->prev = newNode;
83+
size++;
84+
}
85+
}
86+
else
87+
{
88+
printf("Invalid position\n");
89+
}
90+
}
91+
// function for deleting a node at beginning
92+
int DeleteAtBegin()
93+
{
94+
if (head == NULL)
95+
{
96+
printf("List Underflow\n");
97+
return -1;
98+
}
99+
else
100+
{
101+
struct node *ptr = head;
102+
head = head->next;
103+
head->prev = NULL;
104+
int data = ptr->data;
105+
free(ptr);
106+
size--;
107+
return data;
108+
}
109+
}
110+
// function for deleting a node at end
111+
int DeleteFromEnd()
112+
{
113+
if (head == NULL)
114+
{
115+
printf("List Underflow\n");
116+
return -1;
117+
}
118+
else
119+
{
120+
struct node *ptr = tail;
121+
tail = tail->prev;
122+
tail->next = NULL;
123+
int data = ptr->data;
124+
free(ptr);
125+
size--;
126+
return data;
127+
}
128+
}
129+
// function for printing in reverse
130+
void DisplayReversed()
131+
{
132+
if (head == NULL)
133+
{
134+
printf("\nList is empty\n");
135+
}
136+
else
137+
{
138+
printf("\nList in reverse order:-\nNULL");
139+
struct node *ptr = tail;
140+
while (ptr != NULL)
141+
{
142+
printf(" <--> |%d| ", ptr->data);
143+
ptr = ptr->prev;
144+
}
145+
printf("<--> NULL\n");
146+
}
147+
}
148+
// function for search an element
149+
int Search(int data)
150+
{
151+
if (head == NULL)
152+
{
153+
printf("List is empty\n");
154+
return -1;
155+
}
156+
else
157+
{
158+
struct node *ptr = head;
159+
int i = 0;
160+
while (ptr != NULL)
161+
{
162+
if (ptr->data == data)
163+
{
164+
return i;
165+
}
166+
ptr = ptr->next;
167+
i++;
168+
}
169+
return -1;
170+
}
171+
}
172+
// function for print the list
173+
void Display()
174+
{
175+
if (head == NULL)
176+
{
177+
printf("\nList is empty\n");
178+
}
179+
else
180+
{
181+
printf("\nList is :-\nNULL ");
182+
struct node *ptr = head;
183+
while (ptr != NULL)
184+
{
185+
printf(" <--> |%d| ", ptr->data);
186+
ptr = ptr->next;
187+
}
188+
printf("<--> NULL\n");
189+
}
190+
}
191+
int main()
192+
{
193+
int element;
194+
int choice = 0;
195+
int position;
196+
while (choice != 9)
197+
{
198+
printf("\n======================================== MENU========================================\n");
199+
printf("\n1- Insert at beginning");
200+
printf("\t\t\t2- Insert at end");
201+
printf("\t\t\t3- Insert at position");
202+
printf("\n4- Delete from beginning");
203+
printf("\t5- Delete from end");
204+
printf("\t\t\t 6- Print in Reverse Order");
205+
printf("\n7- Search an Element");
206+
printf("\t\t8- Display");
207+
printf("\t\t\t\t 9- Exit");
208+
printf("\n=======================================================================================\n");
209+
printf("\nEnter your choice: ");
210+
scanf("%d", &choice);
211+
switch (choice)
212+
{
213+
case 1:
214+
printf("Enter the element to be inserted: ");
215+
scanf("%d", &element);
216+
InsertAtBegin(element);
217+
Display();
218+
break;
219+
case 2:
220+
printf("Enter the element to be inserted: ");
221+
scanf("%d", &element);
222+
InsertAtEnd(element);
223+
Display();
224+
break;
225+
case 3:
226+
printf("Enter the element to be inserted: ");
227+
scanf("%d", &element);
228+
printf("Enter the position: ");
229+
scanf("%d", &position);
230+
InsertAtPosition(element, position);
231+
Display();
232+
break;
233+
case 4:
234+
printf("Deleted element is: %d\n", DeleteAtBegin());
235+
Display();
236+
break;
237+
case 5:
238+
printf("Deleted element is: %d\n", DeleteFromEnd());
239+
Display();
240+
break;
241+
case 6:
242+
DisplayReversed();
243+
break;
244+
case 7:
245+
printf("Enter the element to be searched: ");
246+
scanf("%d", &element);
247+
if (Search(element) == -1)
248+
{
249+
printf("Element not found\n");
250+
}
251+
else
252+
{
253+
printf("Element found at position: %d\n", Search(element) + 1);
254+
}
255+
break;
256+
case 8:
257+
Display();
258+
break;
259+
case 9:
260+
printf("Exiting...\n");
261+
break;
262+
default:
263+
printf("Invalid choice!!!\n");
264+
}
265+
}
266+
}

counting_sort.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <algorithm>
2+
#include <iostream>
3+
#include <vector>
4+
using namespace std;
5+
6+
void countSort(vector<int>& arr)
7+
{
8+
int max = *max_element(arr.begin(), arr.end());
9+
int min = *min_element(arr.begin(), arr.end());
10+
int range = max - min + 1;
11+
12+
vector<int> count(range), output(arr.size());
13+
for (int i = 0; i < arr.size(); i++)
14+
count[arr[i] - min]++;
15+
16+
for (int i = 1; i < count.size(); i++)
17+
count[i] += count[i - 1];
18+
19+
for (int i = arr.size() - 1; i >= 0; i--) {
20+
output[count[arr[i] - min] - 1] = arr[i];
21+
count[arr[i] - min]--;
22+
}
23+
24+
for (int i = 0; i < arr.size(); i++)
25+
arr[i] = output[i];
26+
}
27+
28+
void printArray(vector<int>& arr)
29+
{
30+
for (int i = 0; i < arr.size(); i++)
31+
cout << arr[i] << " ";
32+
cout << "\n";
33+
}
34+
35+
int main()
36+
{
37+
vector<int> arr = { -5, -10, 0, -3, 8, 5, -1, 10 };
38+
countSort(arr);
39+
printArray(arr);
40+
return 0;
41+
}

0 commit comments

Comments
 (0)