Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary sorting

Rating
-
Sold
-
Pages
5
Uploaded on
27-05-2023
Written in
2022/2023

In these comprehensive notes on data structures in C, you will gain a deep understanding of the fundamental concepts and practical implementation techniques. Whether you're a beginner or an experienced programmer, these notes will equip you with the knowledge and skills to design and manipulate data structures effectively. Explore topics such as arrays, linked lists, stacks, queues, trees, graphs, and more, as you discover how to optimize memory usage and enhance program efficiency. With clear explanations, examples, and code snippets, these notes will empower you to build efficient solutions and elevate your proficiency in data structure manipulation using the C programming language.

Show more Read less
Institution
Course

Content preview

SORTING
sorting is the process of arranging a collection of items or
data elements in a specific order. Sorting is a fundamental
operation in computer science and is used in a wide range of
applications

1.BUBBLE SORT
Bubble sort is a simple comparison-based sorting algorithm.
It repeatedly compares adjacent elements and swaps them
if they are in the wrong order, hence "bubbling" the largest
element towards the end of the array with each iteration. If
the goal is to sort the array in ascending order, the
algorithm starts by comparing the first and second
elements, swapping them if necessary. Then it moves on to
compare the second and third elements, and so on, until the
entire array is sorted.

Bubble sort has an average and worst-case time complexity
of O(n^2), where n is the number of items in the array. This
means that the time it takes to sort the array grows
quadratically with the number of elements, making it
inefficient for large data sets. However, bubble sort has the
advantage of being simple to understand and implement,
making it useful for educational purposes or small data sets
where efficiency is not a primary concern.
The working of the bubble sort algorithm can be
summarized in the following steps:

1. Start with an unsorted array of elements.

2. Compare each pair of adjacent elements from the
beginning of the array.

3. If the elements are in the wrong order (e.g., the current
element is greater than the next element in case of
ascending order), swap them.

4. Repeat steps 2 and 3 until you reach the end of the array.
This ensures that the largest element "bubbles" to the end
of the array.

5. After the first pass, the largest element will be in its
correct position at the end of the array. Repeat steps 2-4 for
the remaining unsorted elements (excluding the last
element which is already in its correct position).

6. Continue the passes until the entire array is sorted. Each
pass will place the next largest element in its correct
position.

7. The array is now sorted in ascending order.

, Here is a visualization of the bubble sort process:

Initial array: [5, 3, 8, 2, 1]

Pass 1:
- Compare 5 and 3. Swap them: [3, 5, 8, 2, 1]
- Compare 5 and 8. No swap.
- Compare 8 and 2. Swap them: [3, 5, 2, 8, 1]
- Compare 8 and 1. Swap them: [3, 5, 2, 1, 8]

Pass 2:
- Compare 3 and 5. No swap.
- Compare 5 and 2. Swap them: [3, 2, 5, 1, 8]
- Compare 5 and 1. Swap them: [3, 2, 1, 5, 8]

Pass 3:
- Compare 3 and 2. Swap them: [2, 3, 1, 5, 8]
- Compare 3 and 1. Swap them: [2, 1, 3, 5, 8]

Pass 4:
- Compare 2 and 1. Swap them: [1, 2, 3, 5, 8]

The array is now sorted: [1, 2, 3, 5, 8].

Note that bubble sort is an inefficient sorting algorithm,
especially for large datasets, due to its time complexity of
O(n^2). However, it is relatively simple to understand and
implement, making it useful for educational purposes or for
small datasets where efficiency is not a major concern.

2.SELECTION SORT
Selection sort is a simple comparison-based sorting
algorithm. It works by dividing the input array into two
portions: the sorted portion and the unsorted portion. In
each iteration, the algorithm finds the minimum (or
maximum) element from the unsorted portion and swaps it
with the first element of the unsorted portion, thus
expanding the sorted portion. This process continues until
the entire array is sorted. Here's how selection sort works:

1. Start with an unsorted array of elements.

2. Iterate through the array from the beginning to the
second-to-last element:
a. Assume the current element is the minimum (or
maximum) element.
b. Compare the assumed minimum (or maximum) element
with each subsequent element in the unsorted portion.
c. If a smaller (or larger) element is found, update the
assumed minimum (or maximum) element accordingly.

Written for

Course

Document information

Uploaded on
May 27, 2023
Number of pages
5
Written in
2022/2023
Type
SUMMARY

Subjects

$14.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
anjithaav

Get to know the seller

Seller avatar
anjithaav university institute of technology
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
14
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions