Skip to content

Commit 97f7133

Browse files
Merge branch 'CodeHarborHub:main' into main
2 parents ed885e7 + 2600b45 commit 97f7133

File tree

71 files changed

+3834
-248
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+3834
-248
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
id: pseudo-class-and-pseudo-elements
3+
title: Pseudo class and Pseudo elements
4+
sidebar_label: Pseudo class and Pseudo elements
5+
tags: [css, introduction, web development, markup language, hyper text, web pages, career opportunities, personal growth, web-development, web design, web pages, websites, career opportunities, contribute to the web, stay relevant, express yourself, learn other technologies, have fun,pseudo classes,pseudo elements]
6+
description: In this tutorial you will learn about Pseudo classes and Pseudo elements in CSS
7+
---
8+
9+
Pseudo-classes are used to define the special states of an element. They are typically prefixed with a colon (`:`).
10+
11+
12+
1. `:hover` : Applies when the user designates an element (with a pointing device), but does not activate it. Often used to change the appearance of a button when the mouse pointer is over it.
13+
14+
```css
15+
button:hover {
16+
background-color: lightblue;
17+
}
18+
```
19+
20+
<BrowserWindow url="http://127.0.0.1:5500/index.html">
21+
<button type="submit">Submit</button>
22+
<style>{`
23+
button:hover {
24+
background-color: lightblue;
25+
}
26+
`}</style>
27+
</BrowserWindow>
28+
29+
30+
2. `:focus` : Applies when an element has received focus (e.g., when clicked on or tabbed to).
31+
32+
```css
33+
input:focus {
34+
border-color: blue;
35+
}
36+
```
37+
38+
<BrowserWindow url="http://127.0.0.1:5500/index.html">
39+
<input type="text" required/>
40+
<style>
41+
{`input:focus {
42+
border-color: blue;
43+
}`}
44+
</style>
45+
</BrowserWindow>
46+
47+
3. `:nth-child(n)` : Matches elements based on their position in a group of siblings.
48+
49+
```css
50+
li:nth-child(2) {
51+
color: green;
52+
}
53+
```
54+
55+
<BrowserWindow url="http://127.0.0.1:5500/index.html">
56+
<ul>
57+
<li>Red</li>
58+
<li>Green</li>
59+
<li>BLue</li>
60+
</ul>
61+
<style>
62+
{`
63+
li:nth-child(2) {
64+
color: green;
65+
}
66+
`}
67+
</style>
68+
</BrowserWindow>
69+
70+
71+
4. `:first-child` : Applies to the first child of its parent.
72+
73+
```css
74+
.container div:first-child {
75+
color: blue;
76+
font-weight: bold;
77+
}
78+
```
79+
80+
<BrowserWindow url="http://127.0.0.1:5500/index.html">
81+
<div className="pseudo_container">
82+
<div>Hello</div>
83+
<div>World</div>
84+
</div>
85+
<style>
86+
{`.pseudo_container div:first-child {
87+
color: blue;
88+
font-weight: bold;
89+
}`}
90+
</style>
91+
</BrowserWindow>
92+
93+
5. `:nth-of-type(n)` : Matches elements based on their position among siblings of the same type.
94+
95+
```css
96+
div:nth-of-type(3)
97+
{
98+
color: red;
99+
}
100+
```
101+
102+
## Pseudo Elements
103+
104+
1. `::before` : Inserts content before an element's actual content.
105+
106+
```css
107+
p::before {
108+
content: "Note: ";
109+
font-weight: bold;
110+
}
111+
```
112+
113+
2. `::after` : Inserts content after an element's actual content.
114+
115+
```css
116+
p::after {
117+
content: " - Read more";
118+
font-style: italic;
119+
}
120+
```
121+
122+
3. `::first-line` : Applies styles to the first line of a block-level element.
123+
124+
```css
125+
p::first-line {
126+
color: red;
127+
font-weight: bold;
128+
}
129+
```

docs/dsa/arrays/bucket-sort.md

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
id: bucket-sort
3+
title: Bucket sort
4+
sidebar_label: Bucket sort
5+
tags:
6+
- DSA
7+
- Python
8+
- C++
9+
- Java
10+
- Sorting
11+
12+
description: "Thsi page containes Bucket Sort, with codes in python, java and c++ "
13+
---
14+
15+
### Introduction to Bucket Sort
16+
17+
Bucket sort is a comparison sorting algorithm that distributes elements into a number of "buckets." Each bucket is then sorted individually, either using another sorting algorithm or recursively applying the bucket sort. Finally, the sorted buckets are combined to form the final sorted array. Bucket sort is particularly useful for uniformly distributed data.
18+
19+
### Steps of Bucket Sort
20+
21+
1. **Create Buckets**: Initialize an empty array of buckets.
22+
2. **Distribute Elements**: Distribute the elements of the input array into the appropriate buckets.
23+
3. **Sort Buckets**: Sort each bucket individually.
24+
4. **Concatenate Buckets**: Concatenate all sorted buckets to form the final sorted array.
25+
26+
### Pseudocode
27+
28+
```text
29+
function bucketSort(array, bucketSize):
30+
if length(array) == 0:
31+
return array
32+
33+
// Determine minimum and maximum values
34+
minValue = min(array)
35+
maxValue = max(array)
36+
37+
// Initialize buckets
38+
bucketCount = floor((maxValue - minValue) / bucketSize) + 1
39+
buckets = array of empty lists of size bucketCount
40+
41+
// Distribute input array values into buckets
42+
for i from 0 to length(array) - 1:
43+
bucketIndex = floor((array[i] - minValue) / bucketSize)
44+
append array[i] to buckets[bucketIndex]
45+
46+
// Sort each bucket and concatenate them
47+
sortedArray = []
48+
for i from 0 to bucketCount - 1:
49+
sort(buckets[i]) // You can use any sorting algorithm
50+
append buckets[i] to sortedArray
51+
52+
return sortedArray
53+
```
54+
55+
### Implementation in Python, C++, and Java
56+
57+
#### Python Implementation
58+
59+
```python
60+
def bucket_sort(numbers, size=5):
61+
if len(numbers) == 0:
62+
return numbers
63+
64+
# Determine minimum and maximum values
65+
min_value = min(numbers)
66+
max_value = max(numbers)
67+
68+
# Initialize buckets
69+
bucket_count = (max_value - min_value) // size + 1
70+
buckets = [[] for _ in range(bucket_count)]
71+
72+
# Distribute input array values into buckets
73+
for number in numbers:
74+
bucket_index = (number - min_value) // size
75+
buckets[bucket_index].append(number)
76+
77+
# Sort each bucket and concatenate them
78+
sorted_numbers = []
79+
for bucket in buckets:
80+
sorted_numbers.extend(sorted(bucket))
81+
82+
return sorted_numbers
83+
84+
# Example usage
85+
data = [42, 32, 33, 52, 37, 47, 51]
86+
sorted_data = bucket_sort(data)
87+
print(sorted_data) # Output: [32, 33, 37, 42, 47, 51, 52]
88+
```
89+
90+
#### C++ Implementation
91+
92+
```cpp
93+
#include <iostream>
94+
#include <vector>
95+
#include <algorithm>
96+
using namespace std;
97+
98+
void bucketSort(vector<int>& nums, int bucketSize) {
99+
if (nums.empty())
100+
return;
101+
102+
// Determine minimum and maximum values
103+
int minValue = *min_element(nums.begin(), nums.end());
104+
int maxValue = *max_element(nums.begin(), nums.end());
105+
106+
// Initialize buckets
107+
int numBuckets = (maxValue - minValue) / bucketSize + 1;
108+
vector<vector<int>> buckets(numBuckets);
109+
110+
// Distribute input array values into buckets
111+
for (int num : nums) {
112+
int bucketIndex = (num - minValue) / bucketSize;
113+
buckets[bucketIndex].push_back(num);
114+
}
115+
116+
// Sort each bucket and concatenate them
117+
nums.clear();
118+
for (auto& bucket : buckets) {
119+
sort(bucket.begin(), bucket.end());
120+
nums.insert(nums.end(), bucket.begin(), bucket.end());
121+
}
122+
}
123+
124+
// Example usage
125+
int main() {
126+
vector<int> data = {42, 32, 33, 52, 37, 47, 51};
127+
bucketSort(data, 5);
128+
for (int num : data) {
129+
cout << num << " ";
130+
}
131+
// Output: 32 33 37 42 47 51 52
132+
return 0;
133+
}
134+
```
135+
136+
#### Java Implementation
137+
138+
```java
139+
import java.util.ArrayList;
140+
import java.util.Collections;
141+
142+
public class BucketSort {
143+
public static void bucketSort(int[] array, int bucketSize) {
144+
if (array.length == 0)
145+
return;
146+
147+
// Determine minimum and maximum values
148+
int minValue = array[0];
149+
int maxValue = array[0];
150+
for (int num : array) {
151+
if (num < minValue)
152+
minValue = num;
153+
else if (num > maxValue)
154+
maxValue = num;
155+
}
156+
157+
// Initialize buckets
158+
int bucketCount = (maxValue - minValue) / bucketSize + 1;
159+
ArrayList<ArrayList<Integer>> buckets = new ArrayList<>(bucketCount);
160+
for (int i = 0; i < bucketCount; i++) {
161+
buckets.add(new ArrayList<Integer>());
162+
}
163+
164+
// Distribute input array values into buckets
165+
for (int num : array) {
166+
int bucketIndex = (num - minValue) / bucketSize;
167+
buckets.get(bucketIndex).add(num);
168+
}
169+
170+
// Sort each bucket and concatenate them
171+
int currentIndex = 0;
172+
for (ArrayList<Integer> bucket : buckets) {
173+
Collections.sort(bucket);
174+
for (int num : bucket) {
175+
array[currentIndex++] = num;
176+
}
177+
}
178+
}
179+
180+
// Example usage
181+
public static void main(String[] args) {
182+
int[] data = {42, 32, 33, 52, 37, 47, 51};
183+
bucketSort(data, 5);
184+
for (int num : data) {
185+
System.out.print(num + " ");
186+
}
187+
// Output: 32 33 37 42 47 51 52
188+
}
189+
}
190+
```
191+
192+
### Complexity
193+
194+
- **Time Complexity**:
195+
196+
- Best Case: $O(n + k)$, where $n$ is the number of elements and $k$ is the number of buckets.
197+
- Average Case: $O(n + k + n \log(\frac{n}{k}))$
198+
- Worst Case: $O(n^2)$, when all elements are placed in one bucket and a slow sorting algorithm (like bubble sort) is used within buckets.
199+
200+
- **Space Complexity**: $O(n + k)$, for the input array and the buckets.
201+
202+
### Conclusion
203+
204+
Bucket sort is efficient for sorting uniformly distributed data and can achieve linear time complexity in the best case. However, it may degrade to quadratic time complexity in the worst case if elements are not uniformly distributed. It's essential to choose an appropriate bucket size and secondary sorting algorithm for optimal performance. By understanding its structure and implementation, you can effectively use bucket sort for various sorting tasks.

0 commit comments

Comments
 (0)