0% found this document useful (0 votes)
101 views10 pages

Solting

C keys as words
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views10 pages

Solting

C keys as words
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Selection Sort

• Algorithm Overview: Selection Sort divides the array into a sorted and an unsorted
part. It repeatedly selects the smallest (or largest) element from the unsorted part and
moves it to the end of the sorted part.
• Time Complexity:
o Best Case: O(n²)
o Average Case: O(n²)
o Worst Case: O(n²)
• Space Complexity: O(1) (in-place sorting)
• Stability: Not stable (equal elements may not retain their relative order)
• Use Cases: Useful for small datasets or when memory usage is a concern.

2. Bubble Sort

• Algorithm Overview: Bubble Sort repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order. This process is
repeated until the list is sorted.
• Time Complexity:
o Best Case: O(n) (when the array is already sorted)
o Average Case: O(n²)
o Worst Case: O(n²)
• Space Complexity: O(1) (in-place sorting)
• Stability: Stable (equal elements retain their relative order)
• Use Cases: Simple to understand and implement; mainly used for educational
purposes.

3. Insertion Sort

• Algorithm Overview: Insertion Sort builds a sorted array one element at a time by
repeatedly taking the next unsorted element and inserting it into the correct position in
the sorted part.
• Time Complexity:
o Best Case: O(n) (when the array is already sorted)
o Average Case: O(n²)
o Worst Case: O(n²)
• Space Complexity: O(1) (in-place sorting)
• Stability: Stable
• Use Cases: Efficient for small data sets and for partially sorted arrays. Commonly
used in practice for small arrays.

4. Quick Sort

• Algorithm Overview: Quick Sort is a divide-and-conquer algorithm that selects a


'pivot' element and partitions the array into two sub-arrays: elements less than the
pivot and elements greater than the pivot. It recursively sorts the sub-arrays.
• Time Complexity:
o Best Case: O(n log n)
o Average Case: O(n log n)
oWorst Case: O(n²) (occurs with poor pivot selection)
• Space Complexity: O(log n) (due to recursive calls, but can be O(n) in the worst case
if recursion stack space is not optimized)
• Stability: Not stable (equal elements may not retain their relative order)
• Use Cases: Very efficient for large datasets and often the algorithm of choice in
practice due to its average-case performance.

Summary Table

Feature Selection Sort Bubble Sort Insertion Sort Quick Sort


Time Complexity O(n²) O(n²) O(n²) O(n log n)
Best Case O(n²) O(n) O(n) O(n log n)
Space Complexity O(1) O(1) O(1) O(log n)
Stability No Yes Yes No
Use Cases Small datasets Educational Small/partially sorted Large datasets

Conclusion

• Selection Sort is easy to implement but inefficient for large datasets due to its O(n²)
time complexity.
• Bubble Sort and Insertion Sort also have O(n²) complexities but offer better
performance in some cases due to their adaptive nature (especially Insertion Sort).
• Quick Sort is preferred for larger datasets due to its average-case efficiency and
lower time complexity, despite its potential worst-case scenario.

When choosing a sorting algorithm, it's important to consider the size of the dataset, the
expected order of elements, and whether stability is a concern.

4o mini

You said:

give example of all


ChatGPT said:

ChatGPT

Let's go over examples of Selection Sort, Bubble Sort, Insertion Sort, and Quick Sort
implemented in C, so you can better understand how each algorithm works. Each sorting
algorithm will be applied to the same array of integers: {64, 25, 12, 22, 11}.
1. Selection Sort Example (C Code)
c
Copy code
#include <stdio.h>

void selectionSort(int arr[], int n) {


int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

void printArray(int arr[], int size) {


for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);

selectionSort(arr, n);
printf("Sorted array using Selection Sort: ");
printArray(arr, n);

return 0;
}

Output:
sql
Copy code
Original array: 64 25 12 22 11
Sorted array using Selection Sort: 11 12 22 25 64
2. Bubble Sort Example (C Code)
c
Copy code
#include <stdio.h>

void bubbleSort(int arr[], int n) {


int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void printArray(int arr[], int size) {


for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);

bubbleSort(arr, n);
printf("Sorted array using Bubble Sort: ");
printArray(arr, n);

return 0;
}

Output:
sql
Copy code
Original array: 64 25 12 22 11
Sorted array using Bubble Sort: 11 12 22 25 64
3. Insertion Sort Example (C Code)

Output:
sql
Copy code
Original array: 64 25 12 22 11
Sorted array using Insertion Sort: 11 12 22 25 64

4. Quick Sort Example (C Code)


c
Copy code
#include <stdio.h>

void swap(int* a, int* b) {


int t = *a;
*a = *b;
*b = t;
}

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

void printArray(int arr[], int size) {


for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);

quickSort(arr, 0, n - 1);
printf("Sorted array using Quick Sort: ");
printArray(arr, n);
return 0;
}

Output:
sql
Copy code
Original array: 64 25 12 22 11
Sorted array using Quick Sort: 11 12 22 25 64

Key Takeaways:

1. Selection Sort is easy to implement but inefficient for large datasets due to its O(n²)
time complexity.
2. Bubble Sort works by repeatedly swapping adjacent elements, but it also has O(n²)
time complexity.
3. Insertion Sort is efficient for small or nearly sorted arrays and performs well with
partially sorted data.
4. Quick Sort is highly efficient for large datasets and performs well on average with
O(n log n) time complexity.

Questions to Consider:

• Do you understand why Quick Sort is generally faster than Selection or Bubble Sort?
• How would you choose the most appropriate sorting algorithm for a specific scenario?

4o mini

You said:

give example
ChatGPT said:

ChatGPT

Here are complete examples of Selection Sort, Bubble Sort, Insertion Sort, and Quick Sort
in C, each sorting the same array of integers {64, 25, 12, 22, 11}.
1. Selection Sort Example
c
Copy code
#include <stdio.h>

// Function to perform selection sort


void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;

// One by one move the boundary of the unsorted subarray


for (i = 0; i < n - 1; i++) {
minIndex = i; // Assume the minimum is the first element
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j; // Update minIndex if a smaller element is
found
}
}
// Swap the found minimum element with the first element of the
unsorted array
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

// Function to print the array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Main function
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


printArray(arr, n);

selectionSort(arr, n);
printf("Sorted array using Selection Sort: ");
printArray(arr, n);

return 0;
}

Output for Selection Sort


sql
Copy code
Original array: 64 25 12 22 11
Sorted array using Selection Sort: 11 12 22 25 64
2. Bubble Sort Example

Output for Bubble Sort


sql
Copy code
Original array: 64 25 12 22 11
Sorted array using Bubble Sort: 11 12 22 25 64

3. Insertion Sort Example


c
Copy code
#include <stdio.h>

// Function to perform insertion sort


void insertionSort(int arr[], int n) {
int i, key, j;

// Traverse through 1 to n
for (i = 1; i < n; i++) {
key = arr[i]; // The element to be positioned
j = i - 1;

// Move elements of arr[0..i-1], that are greater than key,


// to one position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

// Function to print the array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Main function
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


printArray(arr, n);

insertionSort(arr, n);
printf("Sorted array using Insertion Sort: ");
printArray(arr, n);

return 0;
}
Output for Insertion Sort
sql
Copy code
Original array: 64 25 12 22 11
Sorted array using Insertion Sort: 11 12 22 25 64

4. Quick Sort Example


c
Copy code
#include <stdio.h>

// Function to swap two elements


void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}

// Function to perform partitioning


int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Taking the last element as the pivot
int i = (low - 1); // Index of smaller element

for (int j = low; j <= high - 1; j++) {


// If current element is smaller than or equal to pivot
if (arr[j] < pivot) {
i++; // Increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]); // Swap the pivot element with the
element at i + 1
return (i + 1);
}

// Function to perform quick sort


void quickSort(int arr[], int low, int high) {
if (low < high) {
// pi is partitioning index, arr[pi] is now at right place
int pi = partition(arr, low, high);

// Recursively sort elements before and after partition


quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

// Function to print the array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Main function
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


printArray(arr, n);

quickSort(arr, 0, n - 1);
printf("Sorted array using Quick Sort: ");
printArray(arr, n);

return 0;
}

Output for Quick Sort


sql
Copy code
Original array: 64 25 12 22 11
Sorted array using Quick Sort: 11 12 22 25 64

Summary of Outputs

For all the sorting algorithms applied to the same array, the output is consistent:

c
Copy code
Original array: 64 25 12 22 11
Sorted array: 11 12 22 25 64

Key Points

• All algorithms successfully sort the array but have different mechanisms and
performance characteristics.
• Selection Sort, Bubble Sort, and Insertion Sort are simpler and often used for
educational purposes.
• Quick Sort is more efficient for larger datasets due to its average time complexity of
O(n log n).

You might also like