Computer Science

Sorting Algorithms

Sorting algorithms are a set of procedures used to arrange data in a specific order. They are used to sort data in ascending or descending order, and are essential in computer science for efficient data processing and retrieval. There are various types of sorting algorithms, including bubble sort, insertion sort, and quicksort.

Written by Perlego with AI-assistance

10 Key excerpts on "Sorting Algorithms"

  • PHP 7 Data Structures and Algorithms

    Using Sorting Algorithms

    Sorting is one of the most used algorithms in computer programming. Even in our everyday life, if things are not sorted, we can have a hard time with them. Sorting can pave the way for faster searching or ordering of items in a collection. Sorting can be done in many different ways, such as in ascending order or descending order. Sorting can also be based on the type of data. For example, sorting a collection of names will require lexicographical sorting rather than numerical sorting. As sorting can play an important role for other data structures and their efficiencies, there are many different Sorting Algorithms available. We will explore a few of the most popular Sorting Algorithms in this chapter, along with their complexity and usages.
    Passage contains an image

    Understanding sorting and their types

    Sorting means a sorted order of the data. Often, our data is unsorted, which means we need a way to sort it. Usually, sorting is done by comparing different elements with each other and coming up with the ranking. In most cases, without the comparison, we cannot decide on the sorting part. After the comparison, we also need to swap the elements so that we can reorder them. A good sorting algorithm has the characteristics of making a minimum number of comparisons and swapping. There is also non-comparison based sorting, where no comparison is required to sort a list of items. We will also explore those algorithms in this chapter.
    Sorting can be classified into different types based on the type of data set, direction, computational complexities, memory usage, space usage, and so on. Here are few of the Sorting Algorithms we will explore in this chapter:
    • Bubble sort
    • Insertion sort
    • Selection sort
    • Quick sort
    • Merge sort
    • Bucket sort
    We will keep our discussion limited to the preceding list, as they are the most commonly used Sorting Algorithms and can be grouped and classified under different criteria such as simple sorting, efficient sorting, distribution sorting, and so on. We will now explore each of the sorting functionalities, their implementations, and complexity analysis, along with their pros and cons. Let's get started with the most commonly used sorting algorithm - bubble sort.
  • Data Structures and Program Design Using C++
    Sorting refers to the process of arranging the data elements of an array in a specified order , that is, either in ascending or descending order. For example, it will be practically impossible for us to find a name in the telephone directory if the names in it are not in alphabetical order. However, the same can be true for dictionaries, book indexes, bank accounts, and so on. Hence, the convenience of having sorted data is unquestionable. Retrieval of information becomes much easier when the data is stored in some specified order. Therefore, sorting is a very important application in computer science.
    Let us take an array which is declared and initialized as: int array[] = {10, 25, 17, 8, 30, 3} ; Then, the array after applying the sorting technique is: array[] = {3, 8, 10, 17, 25, 30} ;
    A sorting algorithm can be defined as an algorithm which puts the data elements of an array/ list in a certain order, that is, either numerical order or any predefined order. There are many Sorting Algorithms which are available and are widely used according to the different environments required by the different sorting methods.
    The two basic categories of sorting methods are:
    1. Internal Sorting – It refers to the sorting of the data elements stored in the computer’s main memory.
    2. External Sorting – It refers to the sorting of the data elements stored in the files. It is applied when the amount of data is large and cannot be stored in the main memory.
    6.5.1Types of Sorting Methods
    The various sorting methods are:
    1. Selection Sort
    2. Insertion Sort
    3. Merge Sort
    4. Bubble Sort
    5. Quick Sort
    Let us discuss all of them in detail.
    1. Selection Sort
    Selection sort is a sorting technique that works by finding the smallest value in the array and placing it in the first position. After that, it then finds the second smallest value and places it in the second position. This process is repeated until the whole array is sorted. Thus, the selection sort works by finding the smallest unsorted element remaining in the entire array and then swapping it with the element in the next position to be filled. It is a very simple technique, and it is also easier to implement than other sorting techniques. Selection sort is used for sorting files with large records.
  • Essential Algorithms
    eBook - ePub

    Essential Algorithms

    A Practical Approach to Computer Algorithms Using Python and C#

    • Rod Stephens(Author)
    • 2019(Publication Date)
    • Wiley
      (Publisher)
    CHAPTER 6 Sorting
    Sorting Algorithms are usually covered in great detail in algorithms books for several reasons.
    • They are interesting and demonstrate several useful techniques, such as recursion, divide and conquer, heaps, and trees.
    • Sorting Algorithms are well-studied and are some of the few algorithms for which exact run times are known. It can be shown that the fastest possible algorithm that uses comparisons to sort N items must use O(N log N) time. Several Sorting Algorithms actually achieve that performance, so in some sense they are optimal.
    • Sorting Algorithms are useful. Almost any data is more useful when it is sorted in various ways, so Sorting Algorithms play an important role in many applications.
    This chapter describes several different Sorting Algorithms. Some, such as insertionsort, selectionsort, and bubblesort, are relatively simple but slow. Others, such as heapsort, quicksort, and mergesort, are more complicated but much faster. Still others, such as countingsort and pigeonhole sort, don't use comparisons to sort items, so they can break the O(N log N) barrier and perform amazingly fast under the right circumstances.
    The following sections categorize the algorithms by their run-time performance.

    NOTE

    Many programming libraries, such as C# and Python, include sorting tools, and they usually are quite fast. In practice, you may want to use those tools to save time writing and debugging the sorting code. It's still important to understand how Sorting Algorithms work, however, because sometimes you can do even better than the built-in tools. For example, a simple bubblesort algorithm may beat a more complicated library routine for very small lists, and countingsort often beats the tools if the data being sorted has the right characteristics.

    Algorithms

  • Learning JavaScript Data Structures and Algorithms

    Sorting and Searching Algorithms

    Suppose we have a telephone directory (or a notebook) that does not have any sorting order. When you need to add a contact with telephone numbers, you simply write it down in the next available slot. Suppose you also have a high number of contacts in your contact list. On any ordinary day, you need to find a particular contact and his/her telephone number. However, as the contact list is not organized in any order, you have to check it contact by contact until you find the desired one. This approach is horrible, don't you agree? Imagine that you have to search for a contact in Yellow Pages and it is not organized! It could take forever!
    For this reason, among others, we need to organize sets of information, such as the information we have stored in data structures. Sorting and searching algorithms are widely used in the daily problems we have to solve.
    In this chapter, you will learn about the most commonly used sorting and searching algorithms, such as bubble sort, selection sort, insertion sort, shell sort, merge sort, quick sort, counting sort, bucket sort, and radix sort, as well as the sequential, interpolation, and binary search algorithms.
    Passage contains an image

    Sorting Algorithms

    In this section, we will cover some of the most well-known Sorting Algorithms in computer science. We will start with the slowest one, and then we will cover some better algorithms. We will understand that we first need to learn how to sort and then search for any given information.
    You can see an animated version of how the most famous algorithms covered in this chapter work in the following links at https://visualgo.net/en/sorting and https://www.toptal.com/developers/sorting-algorithms .
    Let's get started!
    Passage contains an image

    The bubble sort

    When developers start learning Sorting Algorithms, they usually learn the bubble sort algorithm first, because it is the simplest of all the Sorting Algorithms. However, it is one of the worst-case Sorting Algorithms with respect to runtime, and you will see why.
  • 50 Algorithms Every Programmer Should Know
    eBook - ePub

    50 Algorithms Every Programmer Should Know

    An unbeatable arsenal of algorithmic solutions for real-world problems

    3

    Sorting and Searching Algorithms

    In this chapter, we will look at the algorithms that are used for sorting and searching. This is an important class of algorithms that can be used on their own or can become the foundation for more complex algorithms. These include Natural Language Processing (NLP ) and pattern-extracting algorithms. This chapter starts by presenting different types of Sorting Algorithms. It compares the performance of various approaches to designing a sorting algorithm. Then, some searching algorithms are presented in detail. Finally, a practical example of the sorting and searching algorithms presented in this chapter is studied.
    By the end of this chapter, we should be able to understand the various algorithms that are used for sorting and searching, and we will be able to comprehend their strengths and weaknesses. As searching and Sorting Algorithms are the building blocks for many complex algorithms, understanding them in detail will help us better understand modern complex algorithms as well, as presented in the later chapters.
    The following are the main concepts discussed in this chapter:
    • Introducing Sorting Algorithms
    • Introducing searching algorithms
    • Performance analysis of sorting and searching algorithms
    • Practical applications of sorting and searching
    Let’s first look at some Sorting Algorithms.

    Introducing Sorting Algorithms

    The ability to efficiently sort and search items in a complex data structure is important as it is needed by many modern algorithms. The right strategy to sort and search data will depend on the size and type of the data, as discussed in this chapter. While the end result is exactly the same, the right sorting and searching algorithm will be needed for an efficient solution to a real-world problem. Thus, carefully analyzing the performance of these algorithms is important.
  • Data structures based on non-linear relations and data processing methods
    • Xingni Zhou, Zhiyuan Ren, Yanzhuo Ma, Kai Fan, Xiang Ji(Authors)
    • 2020(Publication Date)
    • De Gruyter
      (Publisher)
    3  Data processing methods – sorting technologies
    Main Contents
    • The concept of sorting
    • Common sorting methods
    Learning Targets
    • Familiarize yourselves with the features of various sorting methods
    • Familiarize yourselves with various sorting processes and their principles.

    3.1  Introduction

    Nowadays, the smartphones store a huge amount of contact information. Most of them are displayed according to the surname of the contact person. We can imagine that if such name information is chaotic and unordered, as shown in Fig. 3.1 , then to find the name of somebody we will need to compare the names one by one, which would be very inconvenient. Also we would need to spend relatively large amount of time when there are relatively more contact persons.
    Fig. 3.1: Unordered contact information.
    In general, we do not want to waste our time on finding certain contact people in the phone, an operation that is supposed to be easy. Therefore, the current mobile operating systems will automatically sort the contact people with a certain order (Chinese character, English character, number all that have their respective orderings), as shown in Fig. 3.2 . In this way, looking up a contact person with a certain name according to the order will save us a lot of time. Then, how can we effectively order some originally chaotic records into sorted orders? This introduces an important problem in computer program design, that is, sorting problem.
    Fig. 3.2: Ordered contact information.

    3.1.1  The basic concepts of sorting

    Sorting is an important operation in computer program design. Its functionality is to re-order an initially unordered arbitrary sequence into a sequence ordered according to the keywords. Apparently, to facilitate lookup, we would normally want a sequence to be nonincreasing (or nondecreasing) ordered. In this manner, we can use highly efficient lookup algorithms to perform the lookup operation. Besides, some basic operations in computer programs, such as constructing binary ordered tree, themselves are processes of sorting. Therefore, to learn and study Sorting Algorithms is an important aspect of computer science students.
  • Beginning Java Data Structures and Algorithms
    eBook - ePub

    Beginning Java Data Structures and Algorithms

    Sharpen your problem solving skills by learning core computer science concepts in a pain-free manner

    Sorting Algorithms and Fundamental Data Structures

    In the previous chapter, we saw how the intersection problem can be improved by using a sorting algorithm. This is common with many problems. If the data is organized in an ordered manner, a more efficient algorithm can be developed. In this chapter, we will start by exploring three types of sorting techniques, which are bubble, quick, and merge sorting. Later, we will learn different ways to organize data using fundamental data structures.
    By the end of this chapter, you will be able to:
    • Describe how bubble sorting works
    • Implement faster sorting with quick sort
    • Characterize merge sorting
    • Build a linked list data structure
    • Implement queues
    • Describe the stack data structure
    Passage contains an image

    Introducing Bubble Sorting

    Bubble sorting is the simplest sorting algorithm out there. The technique involves making multiple passes over the input array and swapping unordered elements close to one another. The technique is called bubble sort, as the sorted list "bubbles" up from the tail end of the list.
    Passage contains an image

    Understanding Bubble Sorting

    All Sorting Algorithms accept a list of elements and return them ordered. The main difference between each algorithm is the manner in which the sorting is done. Bubble sorting works by swapping adjacent elements. This pushes the sorted elements toward the end of the list.
    Snippet 2.1 shows the pseudocode for bubble sort. The algorithm involves three simple tasks, which involves repeatedly stepping through the list to sort, comparing adjacent elements, and swapping them around if the first element is bigger than the second.
    How many passes do we need to perform on the array until our list is sorted? It turns out that to guarantee that our list is sorted, we need to do (n - 1) passes on the list, n being the length of our array. We will show why (n - 1) passes are needed in the next section, but this is the main reason why bubble sort has a runtime complexity of
    O(n2 )
    , since we're processing n elements for n - 1
  • Algorithms: Design Techniques And Analysis (Revised Edition)
    eBook - ePub

    Algorithms: Design Techniques And Analysis (Revised Edition)

    Design Techniques and Analysis(Revised Edition)

    • M H Alsuwaiyel(Author)
    • 2016(Publication Date)
    • WSPC
      (Publisher)
    KRUSKAL for finding a minimum cost spanning tree of an undirected graph. Both data structures are used extensively in the literature for the design of more complex algorithms.
    Passage contains an image

    Chapter 1

    Basic Concepts in Algorithmic Analysis

    1.1    Introduction

    The most general intuitive idea of an algorithm is a procedure that consists of a finite set of instructions which, given an input , enables us to obtain an output if such an output exists or else obtain nothing at all if there is no output for that particular input through a systematic execution of the instructions. The set of possible inputs consists of all inputs to which the algorithm gives an output. If there is an output for a particular input, then we say that the algorithm can be applied to this input and process it to give the corresponding output. We require that an algorithm halts on every input, which implies that each instruction requires a finite amount of time, and each input has a finite length. We also require that the output of a legal input to be unique, that is, the algorithm is deterministic in the sense that the same set of instructions are executed when the algorithm is initiated on a particular input more than once. In Chapter 13 , we will relax this condition when we study randomized algorithms.
    The design and analysis of algorithms are of fundamental importance in the field of computer science. As Donald E. Knuth stated “Computer science is the study of algorithms.” This should not be surprising, as every area in computer science depends heavily on the design of efficient algorithms. As simple examples, compilers and operating systems are nothing but direct implementations of special purpose algorithms.
    The objective of this chapter is twofold. First, it introduces some simple algorithms, particularly related to searching and sorting. Second, it covers the basic concepts used in the design and analysis of algorithms. We will cover in depth the notion of “running time” of an algorithm, as it is of fundamental importance to the design of efficient algorithms. After all, time is the most precious measure of an algorithm’s efficiency. We will also discuss the other important resource measure, namely the space required by an algorithm.
  • Python Data Structures Pocket Primer
    4

    SEARCH AND SORT ALGORITHMS

    T he first half of this chapter provides an introduction to some well-known search algorithms, followed by the second half that discusses various Sorting Algorithms.
    The first section of this chapter introduces search algorithms such as linear search and binary search, that you can use when searching for an item (which can be numeric or character-based) in an array. A linear search is inefficient because it requires an average of n/2 (which has complexity O(n)) comparisons to determine whether or not the search element is in the array, where n is the number of elements in the list or array.
    By contrast, a binary search required O (log n) comparisons, which is vastly more efficient with larger sets of items. For example, if an array contains 1,024 items, a most ten comparisons are required in order to find an element because 2**10 = 1024, so log(1024) = 10. However, a binary search algorithm requires a sorted list of items.
    The second part of this chapter discusses some well-known Sorting Algorithms, such as the bubble sort, selection sort, insertion sort, the merge sort, and the quick sort that you can perform on an array of items.

    SEARCH ALGORITHMS

    The following list contains some well-known search algorithms that will be discussed in several subsections:
    • linear search
    • binary search
    • jump search
    • Fibonacci search
    A linear search algorithm is probably the simplest of all the search algorithms: this algorithm checks every element in an array until either the desired item is located or the end of the array is reached.
    However, as you learned in the introduction for this chapter, a linear search is inefficient when an array contains a large number of values. If the array is very small, the difference in performance between a linear search and a binary search can also be very small; in this case, a linear search might be an acceptable choice of algorithms.
  • Data Structures in Java
    4

    SEARCH AND SORT ALGORITHMS

    T he first half of this chapter provides an introduction to some well-known search algorithms, followed by the second half that discusses various Sorting Algorithms.
    The first section of this chapter introduces search algorithms such as linear search and binary search, that you can use when searching for an item (which can be numeric or character-based) in an array. A linear search is inefficient because it requires an average of n/2 (which has complexity O(n)) comparisons to determine whether or not the search element is in the array, where n is the number of elements in the list or array.
    By contrast, a binary search required O (log n) comparisons, which is vastly more efficient with larger sets of items. For example, if an array contains 1,024 items, a most ten comparisons are required in order to find an element because 2**10 = 1024, so log(1024) = 10. However, a binary search algorithm requires a sorted list of items.
    The second part of this chapter discusses some well-known Sorting Algorithms, such as the bubble sort, selection sort, insertion sort, the merge sort, and the quick sort that you can perform on an array of items.

    SEARCH ALGORITHMS

    The following list contains some well-known search algorithms that will be discussed in several subsections:
    • linear search
    • binary search
    • jump search
    • Fibonacci search
    A linear search algorithm is probably the simplest of all the search algorithms: this algorithm checks every element in an array until either the desired item is located or the end of the array is reached.
    However, as you learned in the introduction for this chapter, a linear search is inefficient when an array contains a large number of values. If the array is very small, the difference in performance between a linear search and a binary search can also be very small. In this case, a linear search might be an acceptable choice of algorithms.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.