International Journal Of Engineering Research & Management Technology

Similar documents
Data Structures and Algorithms Sorting

Sorting & Searching. Hours: 10. Marks: 16

Searching Elements in an Array: Linear and Binary Search. Spring Semester 2007 Programming and Data Structure 1

Computers in Engineering COMP 208. Where s Waldo? Linear Search. Searching and Sorting Michael A. Hawker

Columns A[0] A[0][0] = 20 A[0][1] = 30

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

DATA STRUCTURES AND ALGORITHMS

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

Computer Science Foundation Exam

Lecture 6 Sorting and Searching

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

To become familiar with array manipulation, searching, and sorting.

Sorting. Task Description. Selection Sort. Should we worry about speed?

Searching and Sorting

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

Bubble sort starts with very first two elements, comparing them to check which one is greater.

UNIT 7. SEARCH, SORT AND MERGE

Data Structures and Algorithms

115 cs Algorithms and Data Structures-1. Sorting: To arrange the elements into ascending or descending order.

Chapter 7 Sorting. Terminology. Selection Sort

CSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Sorting. Introduction. Classification

Sorting. Order in the court! sorting 1

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

Introduction to Arrays

PERFORMANCE OF VARIOUS SORTING AND SEARCHING ALGORITHMS Aarushi Madan Aarusi Tuteja Bharti

Chapter 8 Search and Sort

Computer Science 210 Data Structures Siena College Fall Topic Notes: Searching and Sorting

Sorting Algorithms. + Analysis of the Sorting Algorithms

Lecture 7: Sorting Techniques. Prakash Gautam 26 April, 2018

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Data Structure Lecture#16: Internal Sorting (Chapter 7) U Kang Seoul National University

Sorting. Order in the court! sorting 1

Sorting. Chapter 12. Objectives. Upon completion you will be able to:

O(n): printing a list of n items to the screen, looking at each item once.

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

DECISION MAKING STATEMENTS

Questions Bank. 14) State any four advantages of using flow-chart

DATA STRUCTURES AND ALGORITHMS. Sorting algorithms (insertion sort, bubble sort, selection sort)

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Arrays and Strings. Arash Rafiey. September 12, 2017

Data Structures and Algorithms(8)

DATA STRUCTURES AND ALGORITHMS

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

CSE 373: Data Structures and Algorithms

Searching an Array: Linear and Binary Search. 21 July 2009 Programming and Data Structure 1

Cpt S 122 Data Structures. Sorting

CS 3410 Ch 5 (DS*), 23 (IJP^)

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

CS/COE 1501

LECTURE NOTES ON DATA STRUCTURES THROUGH C

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

Announcement. Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00. Caution: Assignment 4 will be due next Wednesday

CSE 373: Data Structures and Algorithms

Keywords: Binary Sort, Sorting, Efficient Algorithm, Sorting Algorithm, Sort Data.

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Programming Assignment #4 Arrays and Pointers

Sorting and Searching. Sudeshna Sarkar 7 th Feb 2017

Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4) Dr. Yingwu Zhu

Sorting and Searching Algorithms

Sorting Algorithms. Biostatistics 615 Lecture 7

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

Bubble sort is so named because the numbers are said to bubble into their correct positions! Bubble Sort

DATA STRUCTURES/UNIT 3

Searching & Sorting in Java Bubble Sort

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Searches and Sorts. ICT Officers. September 2018

Functions. Arash Rafiey. September 26, 2017

CSCI 101: Fundamentals of Computer Programming Lab 11: Strings & Sorts Fall 2005

Loops / Repetition Statements

CS 315 Data Structures mid-term 2

Measuring algorithm efficiency

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

CS 261 Data Structures. Big-Oh Analysis: A Review

Internal Sort by Comparison II

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Searching and Sorting

Sorting. Bringing Order to the World

LECTURE 17. Array Searching and Sorting

Programming 2. Topic 8: Linked Lists, Basic Searching and Sorting

Data Structures. Lecture 6 - Sorts. What is a sort?

Big picture. Definitions. Internal sorting. Exchange sorts. Insertion sort Bubble sort Selection sort Comparison. Comp Sci 1575 Data Structures

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

Fast Bit Sort. A New In Place Sorting Technique. Nando Favaro February 2009

Sorting is ordering a list of objects. Here are some sorting algorithms

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

Internal Sort by Comparison II

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

! Search: find an item in an array, return the. ! Sort: rearrange the items in an array into some. ! There are various methods (algorithms) for

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Internal Sort by Comparison II

Back to Sorting More efficient sorting algorithms

Transcription:

International Journal Of Engineering Research & Management Technology A Study on Different Types of Sorting Techniques ISSN: 2348-4039 Priyanka Gera Department of Information and Technology Dronacharya College of Engineering Gurgaon, Haryana Ashish Gill Department of Information andtechnology Dronacharya College of Engineering Gurgaon, Haryana 1. ABSTRACT Our research paper aims at Sorting Techniques. Sorting is the process of placing elements from a collection in some kind of order. A list of words could be sorted alphabetically or by length. We are going to discuss all types of sorting techniques in our paper so that we can study most of the sorting techniques through our paper. We had not only discussed types of sorting but have also explained each one of them followed with a small piece of program which clearly indicates how each technique is going to work when executed. Sorting really makes our work easy. Without sorting it would be really difficult for us to search for the desired element in the list. But with the help of sorting we can do this in just a few minutes and that too with efficiency. So sorting helps us in our day to day life also. Keywords: External sorting, Internal sorting, in place, Sorting, Stable, Swapping 2. INTRODUCTION The efficiency of data handling can often be substantially increased if the data is sorted according to some criteria of order. For example it would be really difficult to find a name in the telephone directory if the names are not alphabetically arranged. Same case goes with the dictionaries, book indexes, payrolls, bank accounts, student lists and other alphabetically organized materials. It is very convenient for us if our data is in sorted order. So retrieval of information is made easier when it is stored in some predefined order. We have many sorting techniques available to us. We will discuss all types of sorting techniques and will also compare them according to their features and runtime. So sorting is really a important and interesting area to study in. Before getting into specific algorithms, we should think about the operations that can be used to analyze a sorting process. First, it will be necessary to compare two values to see which is smaller (or larger). In order to sort a collection, it will be necessary to have some systematic way to compare values to see if they are out of order. The total number of comparisons will be the most common way to measure a sort procedure. Second, when values are not in the correct position with respect to one another, it may be necessary to exchange them. This exchange is a costly operation and the total number of exchanges will also be important for evaluating the overall efficiency of the algorithm. 3. PROPERTIES OF SORTING TECHNIQUES 3.1 Stable If the sorting algorithm preserves the relative order of any two equal elements, then the sorting order is stable. For example when the cards are sorted by rank with a stable sort, the two 5s must remain in the same order in the sorted output that they were originally in. When they are sorted with a non-stable sort, the 5s may end up in the opposite order in the sorted output. When sorting some kinds of data, only part of the data is examined when determining the sort order. For example, in the card sorting example to the right, the cards are being sorted by their rank, and their suit is being ignored. This allows the possibility of multiple Copyright@ijermt.org Page 263

different correctly sorted versions of the original list. Stable sorting algorithms choose one of these, according to the following rule: if two items compare as equal, like the two 5 cards, then their relative order will be preserved, so that if one came before the other in the input, it will also come before the other in the output. 3.2 In Place If an algorithm does not require an extra memory space except for few memory units, then the algorithm is said to be in place. 4. TYPES OF SORTING 4.1 Internal sorting Internal sorting is applied when the entire collection of data to be sorted is small enough so that the entire sorting can take place within the main memory. The time required to read or write is not considered to be significant in evaluating the performance of internal sorting methods. Internal sorting takes place in the main memory of a computer, where we can use the random access capability of the main memory to advantage in various ways. By internal sorting we are arranging the numbers within the array only which is in computer main memory. 4.2 External sorting When large data have to be sorted they cannot be brought into the main memory at the same time. In such situations we use external sorting where the entire file may reside on some secondary storage device. At the same time based on the availability of the storage space in the main memory, some portion of the data from the secondary storage device will be brought into the main memory that will be sorted and sent back to the secondary storage device. Then the next portion will be brought into the main memory, sorted and then sent to the secondary storage device till the time when whole of the file which is there on the secondary storage device is sorted. 5. TYPES OF INTERNAL SORTING 5.1 Bubble Sort Bubble sort, also known as exchange sort, is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted. Comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which means the list is sorted. The algorithm gets its name from the way smaller elements bubble to the top of the list via swaps. In this method we assume that the lower value items from the set of items are light and bubble on to the top. The bubble sort is generally considered to be the most efficient sorting algorithm in common usage. 5.1.1 Code for bubble sort Void bubble ( int x[ ], int n) int i, j, tmp; for(i=0; i<n; i++) for(j=0; j<n-1; j++) if(x[j] > x[j+1]) tmp=x[j]; x[j] = x[j+1]; x[j+1] = tmp; Copyright@ijermt.org Page 264

5.2 Selection Sort The idea of selection sort is rather simple; we repeatedly find the next largest or smallest element in the array and move it to its final position in the sorted array. For example if we need to sort the array in increasing order we will begin by selecting the largest element and moving it to the highest index position. We can do this by swapping the element at the highest index and the largest element. We then reduce the effective size of the array by one element and repeat the process on the smaller sub array. The process stops when the effective size of the array becomes 1. Thus, the selection sort works by selecting the smallest unsorted item remaining in the list, and then swapping it with the item in the next position to be filled. This is the simple method of sorting. 5.2.1 Code for selection sort Void selection ( int x[ ], int n) int i, j, larg, pos, c=0; for ( i = n-1; i > 0; i--) larg = x[0]; pos=0; for(j = 1; j<=i; j++) c++; if ( x[j] > larg) larg = x[j]; pos = j; x[pos] = x[i]; x[i] = larg; printf( \n number of comparisons required = %d,c); 5.3 Insertion Sort The insertion sort works just like its name suggests it- it inserts each item in its proper place in the final list. The simplest implementation of this requires two list structures the source list and the list into which sorted items are inserted. To save memory, most implementations use an in place sort that works by moving the current item past the already sorted items and repeatedly swapping it with the preceding item until it is in place. This method is widely used by card players. 6.3.1 Code for insertion sort Void insertion ( int x[ ], int n) int i, j, tmp; for( i=0; i<n; i++) Copyright@ijermt.org Page 265

tmp = x[i]; for( j= i-1; j>=0; j--) if( temp < x[j]) x[j+1] = x[j]; else break; x[j+1] = temp; 5.4 Shell Sort Shell sort is the modification of the insertion sort. Shell sort improves on the efficiency of insertion sort by quickly values to their destination. In this instead of sorting the entire array at once the array is first divided into smaller segments. Then these segments are sorted separately using the insertion sort. 5.4.1 Code for shell sort #define MAX 15 Void main ( ) int arr[max], i, j, k, n, incr; printf( enter no of elements ); scanf( %d, &n); for ( i=0; i<n; i++) printf( enter element %d, i+1); scanf( %d, &arr[i]); printf( unsorted list is ); for( i=0; i<n; i++) printf( %d, arr[i]); printf( enter maximum increment (odd value) ); scanf( %d, &incr); /* shell sort algorithm applied */ while( incr >=1) for( j=incr; j<n; j++) k=arr[ i]; for( i = j- incr; i>=0 && k<arr[i]; i = i-incr) arr[ i+incr]= arr[i]; arr[ i +incr]=k; printf( increment = %d, incr); for ( i=0; i<n; i++) printf( %d, arr[i]); incr= incr-2; Copyright@ijermt.org Page 266

printf sorted list is ) for(i=0; i<n; i++) printf( %d, arr[i]); 5.5 Merge Sort Merge sort is a algorithm that uses the idea of divide and conquer approach. 5.5.1 Code for merge sort through recursion # define MAX 20 int array [MAX]; Void merge ( int low, int mid, int high) int temp[max; int i= low; int j= mid+1; int k= low; While (( i<= mid) && (j <= high)) if ( array[i]<=array[j]) temp [k++]= array[i++]; else temp [k++]= array[j++]; While ( i<=mid) temp[k++]= array[i++]; while (j<= high) temp[k++]= array[j++]; while (j<=high) temp[k++]= array[j++]; for (i=low; i<= high; i++) Array[i]= temp[i]; Void merge_ sort( int low, int high) int mid; if (low!= high) mid=(low+ high)/2; merge_ sort(low,mid); merge_ sort(mid+1, high); merge(low, mid, high ); Copyright@ijermt.org Page 267

6. CONCLUSION In our day to day life we come across many such instances where we literally need sorting. Without sorting it would be very difficult to search a particular thing in the list. At the same time it would be really time consuming as it would take a lot of time if we are going to search a particular thing in our list one by one. So sorting is the best option in that case in which we can arrange elements according to our wish. Sorting makes our task really convenient, easy and efficient. Manually there are chances that we may commit a mistake but if we are following the code of any type of sorting algorithm then there are the minimal chances of any kind of mistake. So sorting holds a very important place in computer science. 7. REFERENCES 1. Search engines like Google, Wikipedia 2. ACM transactions on algorithms 3. Various books on Data Structures 5. The sorting techniques: a tutorial paper on card sorts, picture sorts and item sort Copyright@ijermt.org Page 268