Sorting Algorithms in C

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which sorting algorithm is known for its simplicity in implementation?

  • Bubble Sort (correct)
  • Heap Sort
  • Shell Sort
  • Quicksort

After the first pass of Bubble Sort, what is guaranteed to be in its correct position?

  • The smallest value
  • The middle value
  • All values
  • The largest value (correct)

What is the time complexity of Bubble Sort in the average and worst case scenarios?

  • O(n^2) (correct)
  • O(log n)
  • O(n log n)
  • O(n)

Under what condition is Bubble Sort considered to be relatively fast?

<p>When the list is nearly sorted (A)</p> Signup and view all the answers

What is the primary operation Selection Sort performs to arrange elements?

<p>Finding the smallest number and moving it to the beginning (B)</p> Signup and view all the answers

How does Selection Sort compare to Bubble Sort in terms of the number of swaps performed?

<p>Selection Sort generally does fewer swaps. (D)</p> Signup and view all the answers

What is the typical time complexity of Selection Sort?

<p>O(n^2) (C)</p> Signup and view all the answers

For nearly sorted data, which sorting algorithm might be a good choice?

<p>Insertion Sort (D)</p> Signup and view all the answers

What is a common use of Insertion Sort in more advanced sorting algorithms?

<p>As a recursive base case in divide-and-conquer algorithms (C)</p> Signup and view all the answers

What is the worst-case time complexity of Insertion Sort?

<p>O(n^2) (A)</p> Signup and view all the answers

Which data structure does Heap Sort primarily interpret the data as?

<p>A binary tree (A)</p> Signup and view all the answers

What property defines a (max) heap within the context of Heap Sort?

<p>The root is greater than or equal to its children. (D)</p> Signup and view all the answers

What is the typical time complexity of Heap Sort?

<p>O(n log n) (D)</p> Signup and view all the answers

In which case does Heap Sort perform as fast as possible?

<p>In a reversed case (C)</p> Signup and view all the answers

What is the first step in the Heap Sort algorithm?

<p>Converting the partial tree rooted there to a max-heap, starting at the last non-leaf node entered. (B)</p> Signup and view all the answers

What is the primary strategy employed by Quicksort to sort a list?

<p>Dividing the list into partitions around a pivot (B)</p> Signup and view all the answers

What is the role of the 'pivot' in the Quicksort algorithm?

<p>To serve as a reference point for partitioning the list (D)</p> Signup and view all the answers

What happens after the list is split at the pivot's new position in Quicksort?

<p>The same process is used to sort the two halves, using recursion. (C)</p> Signup and view all the answers

What is the best-case time complexity of Quicksort?

<p>O(n log n) (B)</p> Signup and view all the answers

What can happen if the smallest number is consistently chosen as the pivot in Quicksort?

<p>The algorithm's performance degrades to that of Selection Sort. (C)</p> Signup and view all the answers

Which sorting algorithm works by comparing elements a certain number of steps apart?

<p>Shell Sort (D)</p> Signup and view all the answers

What is a key feature of the gap sequence in Shell Sort?

<p>The gap size starts large and decreases to smaller sizes. (B)</p> Signup and view all the answers

Which sorting algorithm is easier to implement and takes less memory than Heapsort or Quicksort?

<p>Shell Sort (C)</p> Signup and view all the answers

What effect does each incremental sort have on the list in Shell Sort?

<p>It brings the list into a 'closer-to-sorted' order (D)</p> Signup and view all the answers

In general, if you don't know the nature of the data you need to sort, which sorting algorithm is often recommended?

<p>Quicksort (D)</p> Signup and view all the answers

If you know the data you need to sort is 'almost reversed' what should you do?

<p>Find the best algorithm by doing some research (C)</p> Signup and view all the answers

What is the primary reason smart programmers avoid writing sorting algorithms from scratch?

<p>The chance of in-house algorithm bugs is high (A)</p> Signup and view all the answers

What sorting algorithm does Java's Arrays.sort() method use?

<p>Dual-Pivot Quicksort (D)</p> Signup and view all the answers

What type of algorithm does the C library function qsort() usually use with a good way to chose a pivot?

<p>Quicksort (C)</p> Signup and view all the answers

What must you write to use qsort when comparing two values in your array?.

<p>A function (A)</p> Signup and view all the answers

In a comparison function used with qsort(), what integer value should be returned if the first parameter is considered less than the second?

<p>-1 (B)</p> Signup and view all the answers

What does the comparison function do internally?.

<p>Cast the void pointers to the correct data type (D)</p> Signup and view all the answers

Consider the function prototype for qsort in C: void qsort(void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*));. What does the nmemb parameter represent?

<p>Number of elements in the array (D)</p> Signup and view all the answers

Suppose you have a linked list of 'Student' structures. How can we sort this linked list?

<p>Without using an array by adjusting next pointers to swap adjacent nodes. (B)</p> Signup and view all the answers

Swapping in a linked lists requires changing how many next pointers?

<p>Three (A)</p> Signup and view all the answers

When is sorting linked data structures more efficient?

<p>When the size of each node structure is large (A)</p> Signup and view all the answers

Can other sort algorithms be applied to linked lists?

<p>Yes, but would be even more complicated. (C)</p> Signup and view all the answers

Given the following code snippet performing a bubble sort-like operation on a linked list in C, identify the most accurate reason why the logic to update 'last->next' is crucial for the algorithm's correctness. Assume *nxx represents st->next->next, 'last' is the last 'st' processed in the inner loop, and 'st' and 'st->next' are being compared:

if (last != NULL)
    last->next = st->next; // (green)
else
    list->head = st->next; // Update head if needed

<p>It correctly links the previous node (pointed to by 'last') to the new successor in the event of a swap, maintaining list integrity; also updates the list's head if the swap occurs at the beginning. (C)</p> Signup and view all the answers

Flashcards

What is Bubble Sort?

Iterates through a list, swapping adjacent values if they are out of order.

What happens after the first pass of Bubble Sort?

After the first pass, the largest value is at the end.

What is Selection Sort?

An easy sorting algorithm where the smallest number is found and moved to the beginning of the list. This process is repeated for the remaining unsorted portion of the list.

What is Insertion Sort?

Each number is taken in turn and inserted into the partially sorted list in the correct place until the list is completely sorted.

Signup and view all the flashcards

What is Heap Sort?

Interprets the data as an almost complete binary tree. A max heap root is greater than or equal to children values, and children are also heaps.

Signup and view all the flashcards

How does Heap Sort Algorithm typically work?

When using Heap Sort, start at last non-leaf node, convert partial tree to max-heap, go to next non-leaf node to the left and make max-heap.

Signup and view all the flashcards

What is Quicksort?

Partitions a list with respect to a pivot, placing smaller numbers before it and larger numbers after it. This process is then recursively applied to the sublists.

Signup and view all the flashcards

What is Shell Sort?

Compares elements a certain number of steps apart, swapping if needed. Begins with larger steps and moves to smaller ones, creating a closer-to-sorted order quickly.

Signup and view all the flashcards

What should all programmers do?

Avoid writing sorting algorithms if at all possible. Instead, use sorting functions supplied in your language library.

Signup and view all the flashcards

What does base do in qsort?

Points to the start of an array.

Signup and view all the flashcards

What does nmemb do in qsort?

The number of values in the array.

Signup and view all the flashcards

What does size do in qsort?

Size in bytes of each element in the array.

Signup and view all the flashcards

What does comparison function (compar) do in qsort?

A function that takes two parameters to compare, returning an integer.

Signup and view all the flashcards

What does a comparison function in qsort need to return?

-1 if a<b, 0 if a==b, 1 if a>b

Signup and view all the flashcards

Bubble Sort in linked lists requires what?

Changing three next pointers to swap nodes.

Signup and view all the flashcards

What is the Big O of Bubble Sort?

O(n^2)

Signup and view all the flashcards

What is the Big O of Selection Sort?

O(n^2)

Signup and view all the flashcards

What is the Big O of Insertion Sort?

O(n^2)

Signup and view all the flashcards

What is the Big O of Heap Sort?

O(n log(n))

Signup and view all the flashcards

What is the Big O of Quicksort?

O(n log(n)) (but can degrade to O(n^2))

Signup and view all the flashcards

What is the Big O of Shellsort?

Between O(n log(n)) and O(n^2)

Signup and view all the flashcards

Study Notes

  • Study notes on sorting algorithms and data structures in C

Sorting Algorithms

  • There are several sorting algorithms will be covered
  • These include bubble sort, selection sort, insertion sort, heap sort, quicksort, and shell sort.
  • Visualizations of different sorting algorithms can be found at:
    • https://www.toptal.com/developers/sorting-algorithms/
    • http://www.bluffton.edu/homepages/facstaff/nesterd/java/SortingDemo.html
  • The usefulness of each algorithm can vary based on the data being sorted, such as size, randomness, or existing order
  • All algorithms perform the same task to sort the data

Bubble Sort

  • This is one of the easiest sorting methods to implement
  • It continually swaps adjacent values within a list to order it
  • The largest value will bubble to the end of the list after the first pass
  • The sort completes after a pass where no swaps occur
  • Bubble sort is likely the worst sort in terms of performance, with O(n²) time complexity,
  • it typically induces a significant number of swaps
  • This method is fast if the list is nearly sorted, potentially performing at O(n)

Selection Sort

  • Similar to bubble sort it is easy to implement
  • It finds the smallest number in the list and moves it to the beginning
  • Then repeats the process for the remain list
  • Essentially, it finds the next smallest number and puts it next in the sorted list
  • Each time, the largest number could be found and moved to the end
  • Regarding performance, it is almost as bad as bubble sort
  • Although it does very few swaps, selection sort always runs in O(n²) time
  • On the same data, selection sort is usually faster than bubble sort due to fewer swaps, the difference isn't significant
  • They are both O(n²)

Insertion Sort

  • Insertion sort involves inserting each number into its correct position within a partially sorted list
  • It iterates through the list to insert each number where it needs to go
  • Very similar to performing a selection sort
  • One possible implementation is to find an element that's out of order and then move existing elements to make room for it
  • Insertion sort also runs in O(n²) worst-case time
  • This can be useful if the data set is nearly sorted.
  • It sometimes acts as a recursive base case for quicksort or merge sort divide-and-conquer algorithms
  • If the data is nearly sorted, insertion sort may take O(n) time, but this is uncommon by itself

Heap Sort

  • Heap sort is interpreting data as a complete binary tree
  • A max heap is a binary tree where the root is greater than or equal to its children, which are also heaps
  • The data is organized without particular order, starting at the root, then the left and right children in order
  • Starting from the last nonleaf node, convert the partial tree rooted there to a max-heap, which may require swaps
  • The max-heap has the largest number as root
  • Next, go to the next non-leaf node to the left and make the partial tree there a max-heap
  • Once the left side of the tree is reached, start over on the right at the level above and repeat the process
  • If that wasn’t tricky enough, move the node at the root (top) to the last node in the heap
  • It could also just remove it, move it to a list or queue
  • Re max-heap the new tree
  • Place the new root in the next node to the left and re max-heap, etc
  • It runs in order O(n log(n)), but is considered quite good
  • In a nearly sorted case, heap sort could destroy the original order
  • In a reversed case, heap sort is as fast as possible
  • Requires first loop to put data in heap order
  • Second loop re-heaps the data repeatedly after extracting the maximum each time
  • If a computer processes one million comparisons per second and needs to sort one million items, a selection sort takes approximately 12 days
  • With a heap sort, this process takes about 20 seconds

Quicksort

  • Algorithm that works by partitioning a list with regard to a selected pivot
  • The pivot is a number in the list used to organize the other values
  • The pivot can be the last, middle or a random number.
  • Numbers smaller than the pivot are moved to the front, followed by the pivot, then the larger numbers.
  • The list is then split at the pivot's new position
  • Recursive calls are then made using the same process on the 2 halves
  • Each number in the array is looked at up to the pivot
  • Bigger numbers are left alone, smaller numbers are swapped with the split position
  • The split position starts at zero, and increments by one as numbers are swapped
  • There is then one more swap between the pivot and the number at the split position
  • Divide the list at the split position and perform quicksort on each part recursively
  • The computational complexity and performance of Quicksort can be unclear
  • Technically, its O(n log(n)), but its performance relies on how you choose the pivot
  • If the smallest number is the initial pivot, then it will perform nearly as badly as a selection sort, O(n²)
  • Pivot can be a random number, median number, or have a dual pivot.
  • What works well depends on the properties of data and how it is sorted

Shell Sort

  • Donald Shell named this sort that divides elements by certain number of steps while swapping if needed
  • Initiate with larger steps that turn into smaller ones
  • With an example comparison between all elements that have 103 elements for their gap (gap of 103)

Shell Sort Complexity

  • Difficult algorithm to analyze, shell sort is not as fast as a heap sort or quick sort
  • However, shell sort is easier to implement and requires less memory
  • The idea of shell sort is that after each incremental sort, the list will be in a "closer-to-sorted" order
  • Next steps in shell sort will take less time overall
  • Given code uses "not so good" gap sequence, first gap sixe is half the data size, and next gap is half that, and so on
  • The overall computational complexity of Shell Sort depends on the gap sequence
  • O(n²) is the complexity for simple gap sequence
  • Better gap sequence such as Tokuda's has unknown but better complexity of O(n4/3).
  • This is not as good as O(n log(n))

Other Topics

  • Bubble sort: O(n²), requires many swaps and is slow
  • Selection sort: O(n²), requires very few swaps
  • Insertion sort: O(n²), fast with nearly sorted list
  • Heap sort: O(nlog(n)) always, but is complex
  • Quicksort: O(n log(n)) but sometimes O(n²), efficient
  • Shell sort: ~ O(n4/3) with good gap sequence, needs very little memory
  • In general, if data nature unknown, sort with quicksort
  • If the data nature is known, research and find the best algorithm
  • For programmers, avoid writing sorting Algorithms
  • Sorting functions are often made available as part of the language library
  • Java has Arrays.sort()
  • Is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch that can have O(n log(n)) performance
  • C uses qsort() function and is implemented differently on different systems
  • Usually uses Quicksort with good way to choose pivot, e.g Cygwin while Linux uses a Merge Sort algorithm
  • Code is also here
  • C: lets you pass function pointers as parameters to functions -The closest thing in Java is Lambda expressions

C library qsort function

  • The Function prototype for qsort:
    • void qsort(void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*));
  • base points to start of an array
  • nmemb is the number of values in an array
  • size is the size in bytes of each element of the array
  • compar is a comparison function that takes two parameters to compare and has returns
  • When using qsort a function must be written to compare two of the values

C Code Information

  • An integer must be returned by the comparison function
  • Negative number when parameter 1 is less than 2
  • Zero when parameters are equal
  • A Positive Number when parameter one is larger than two
  • Name of the function should be placed as the last perimeter to qsort with no parenthesizes.

Sorting a linked list

  • Sorting linked data structures is efficient when the size of each node structure is large
  • There is no need to copy the node contents as with an array, just change next pointers
  • Other sort algorithms are possible, but would be even more complicated
  • To sort a linked list that swaps elements like bubble sort, need to adjust next pointers to swap adjacent nodes

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Bubble Sort Algorithm Quiz
5 questions
Bubble Sort Algorithm Overview
6 questions
Algoritma Pengurutan: Bubble Sort
21 questions
Use Quizgecko on...
Browser
Browser