0% found this document useful (0 votes)
3 views

question - Copy

The document outlines various sorting techniques and problems, including basic sorting, sorting by absolute value, and custom sorting functions. It provides examples in Python for each technique, such as Bubble Sort, Merge Sort, and Quick Sort, along with sorting lists of dictionaries and strings. Additionally, it covers advanced topics like Radix Sort and sorting based on multiple criteria.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

question - Copy

The document outlines various sorting techniques and problems, including basic sorting, sorting by absolute value, and custom sorting functions. It provides examples in Python for each technique, such as Bubble Sort, Merge Sort, and Quick Sort, along with sorting lists of dictionaries and strings. Additionally, it covers advanced topics like Radix Sort and sorting based on multiple criteria.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Basic Sorting

Problem: Sort a list of numbers in ascending or descending order.


Example:
python
Copy
Edit
numbers = [12, 3, 5, 7, 19, 10]
sorted_numbers = sorted(numbers) # Ascending order
print(sorted_numbers)
2. Sort by Absolute Value
Problem: Sort a list of numbers by their absolute value.
Example:
python
Copy
Edit
numbers = [-9, -3, 7, 5, 2]
sorted_numbers = sorted(numbers, key=abs)
print(sorted_numbers)
3. Sort in Custom Order
Problem: Sort a list of numbers based on a custom sorting function.
Example: Sort numbers by their number of divisors.
python
Copy
Edit
def count_divisors(n):
count = 0
for i in range(1, n + 1):
if n % i == 0:
count += 1
return count

numbers = [12, 5, 8, 9, 7]
sorted_numbers = sorted(numbers, key=count_divisors)
print(sorted_numbers)
4. Sort in Reverse Alphabetical Order
Problem: Sort a list of strings in reverse alphabetical order.
Example:
python
Copy
Edit
words = ['apple', 'banana', 'cherry', 'date']
sorted_words = sorted(words, reverse=True)
print(sorted_words)
5. Sort Using Lambda Function
Problem: Sort a list of tuples by the second element.
Example:
python
Copy
Edit
tuples = [(1, 3), (2, 2), (3, 1)]
sorted_tuples = sorted(tuples, key=lambda x: x[1])
print(sorted_tuples)
6. Sort List of Dictionaries
Problem: Sort a list of dictionaries by a specific key.
Example:
python
Copy
Edit
students = [
{'name': 'Alice', 'score': 90},
{'name': 'Bob', 'score': 82},
{'name': 'Charlie', 'score': 88}
]
sorted_students = sorted(students, key=lambda x: x['score'], reverse=True)
print(sorted_students)
7. Sort List of Strings by Length
Problem: Sort a list of strings by their length.
Example:
python
Copy
Edit
words = ['banana', 'apple', 'kiwi', 'cherry', 'grape']
sorted_words = sorted(words, key=len)
print(sorted_words)
8. Bubble Sort Implementation
Problem: Implement the Bubble Sort algorithm manually.
Example:
python
Copy
Edit
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr

numbers = [64, 34, 25, 12, 22, 11, 90]


sorted_numbers = bubble_sort(numbers)
print(sorted_numbers)
9. Merge Sort
Problem: Implement Merge Sort manually.
Example:
python
Copy
Edit
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]

merge_sort(left_half)
merge_sort(right_half)

i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1

while i < len(left_half):


arr[k] = left_half[i]
i += 1
k += 1

while j < len(right_half):


arr[k] = right_half[j]
j += 1
k += 1

return arr

numbers = [38, 27, 43, 3, 9, 82, 10]


sorted_numbers = merge_sort(numbers)
print(sorted_numbers)
10. Quick Sort
Problem: Implement the Quick Sort algorithm manually.
Example:
python
Copy
Edit
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[0]
less = [x for x in arr[1:] if x <= pivot]
greater = [x for x in arr[1:] if x > pivot]
return quick_sort(less) + [pivot] + quick_sort(greater)

numbers = [10, 7, 8, 9, 1, 5]
sorted_numbers = quick_sort(numbers)
print(sorted_numbers)
11. Sort a List of Lists
Problem: Sort a list of lists by the sum of each inner list.
Example:
python
Copy
Edit
lists = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
sorted_lists = sorted(lists, key=lambda x: sum(x))
print(sorted_lists)
12. Top N Elements
Problem: Find the N largest or smallest numbers in a list.
Example:
python
Copy
Edit
import heapq

numbers = [12, 5, 7, 19, 1, 8, 3]


top_3_largest = heapq.nlargest(3, numbers)
print(top_3_largest)
13. Sort Based on Multiple Criteria
Problem: Sort a list of tuples first by the first element, then by the second
element.
Example:
python
Copy
Edit
tuples = [(1, 3), (3, 2), (1, 2), (2, 1)]
sorted_tuples = sorted(tuples, key=lambda x: (x[0], x[1]))
print(sorted_tuples)
14. Radix Sort
Problem: Implement the Radix Sort algorithm.
Example:
python
Copy
Edit
def counting_sort(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10

for i in range(n):
index = arr[i] // exp
count[index % 10] += 1

for i in range(1, 10):


count[i] += count[i - 1]

i = n - 1
while i >= 0:
index = arr[i] // exp
output[count[index % 10] - 1] = arr[i]
count[index % 10] -= 1
i -= 1

for i in range(n):
arr[i] = output[i]

def radix_sort(arr):
max_element = max(arr)
exp = 1
while max_element // exp > 0:
counting_sort(arr, exp)
exp *= 10
return arr

numbers = [170, 45, 75, 90, 802, 24, 2, 66]


sorted_numbers = radix_sort(numbers)
print(sorted_numbers)
15. Sort with Key Function (Custom Sorting Criteria)
Problem: Sort a list of strings based on the number of vowels in each string.
Example:
python
Copy
Edit
def count_vowels(s):
vowels = 'aeiou'
return sum(1 for char in s if char in vowels)

words = ['banana', 'apple', 'grape', 'kiwi']


sorted_words = sorted(words, key=count_vowels)
print(sorted_words)
These problems will help you practice different sorting techniques and scenarios.
You can experiment with modifying these examples to fit your needs. Happy coding!
1.Basic Sorting Problem: How do you sort a list of numbers in ascending or
descending order?

2.Sort by Absolute Value Problem: How do you sort a list of numbers by their
absolute value?

3.Sort in Custom Order Problem: How do you sort a list of numbers based on a custom
sorting function like the number of divisors?

4.Sort in Reverse Alphabetical Order: How do you sort a list of strings in reverse
alphabetical order?

5.Sort Using Lambda Function: How do you sort a list of tuples by the second
element using a lambda function?

6.Sort List of Dictionaries: How do you sort a list of dictionaries by a specific


key?

7.Sort List of Strings by Length: How do you sort a list of strings by their
length?

8.Bubble Sort Implementation: How do you implement the Bubble Sort algorithm
manually?

9.Merge Sort: How do you implement the Merge Sort algorithm manually?

10.Quick Sort: How do you implement the Quick Sort algorithm manually?

11.Sort a List of Lists: How do you sort a list of lists by the sum of each inner
list?

12.Top N Elements: How do you find the N largest or smallest numbers in a list?

13.Sort Based on Multiple Criteria: How do you sort a list of tuples first by the
first element, then by the second element?

14.Radix Sort: How do you implement the Radix Sort algorithm?

15.Sort with Key Function (Custom Sorting Criteria): How do you sort a list of
strings based on the number of vowels in each string?

You might also like