Wednesday, July 8, 2020

Sorting Algorithms In C

Sorting Algorithms In C Everything You Need To Know About Sorting Algorithms In C Back Home Categories Online Courses Mock Interviews Webinars NEW Community Write for Us Categories Artificial Intelligence AI vs Machine Learning vs Deep LearningMachine Learning AlgorithmsArtificial Intelligence TutorialWhat is Deep LearningDeep Learning TutorialInstall TensorFlowDeep Learning with PythonBackpropagationTensorFlow TutorialConvolutional Neural Network TutorialVIEW ALL BI and Visualization What is TableauTableau TutorialTableau Interview QuestionsWhat is InformaticaInformatica Interview QuestionsPower BI TutorialPower BI Interview QuestionsOLTP vs OLAPQlikView TutorialAdvanced Excel Formulas TutorialVIEW ALL Big Data What is HadoopHadoop ArchitectureHadoop TutorialHadoop Interview QuestionsHadoop EcosystemData Science vs Big Data vs Data AnalyticsWhat is Big DataMapReduce TutorialPig TutorialSpark TutorialSpark Interview QuestionsBig Data TutorialHive TutorialVIEW ALL Blockchain Blockchain TutorialWhat is BlockchainHyperledger FabricWhat Is EthereumEthereum TutorialB lockchain ApplicationsSolidity TutorialBlockchain ProgrammingHow Blockchain WorksVIEW ALL Cloud Computing What is AWSAWS TutorialAWS CertificationAzure Interview QuestionsAzure TutorialWhat Is Cloud ComputingWhat Is SalesforceIoT TutorialSalesforce TutorialSalesforce Interview QuestionsVIEW ALL Cyber Security Cloud SecurityWhat is CryptographyNmap TutorialSQL Injection AttacksHow To Install Kali LinuxHow to become an Ethical Hacker?Footprinting in Ethical HackingNetwork Scanning for Ethical HackingARP SpoofingApplication SecurityVIEW ALL Data Science Python Pandas TutorialWhat is Machine LearningMachine Learning TutorialMachine Learning ProjectsMachine Learning Interview QuestionsWhat Is Data ScienceSAS TutorialR TutorialData Science ProjectsHow to become a data scientistData Science Interview QuestionsData Scientist SalaryVIEW ALL Data Warehousing and ETL What is Data WarehouseDimension Table in Data WarehousingData Warehousing Interview QuestionsData warehouse architectureTalend T utorialTalend ETL ToolTalend Interview QuestionsFact Table and its TypesInformatica TransformationsInformatica TutorialVIEW ALL Databases What is MySQLMySQL Data TypesSQL JoinsSQL Data TypesWhat is MongoDBMongoDB Interview QuestionsMySQL TutorialSQL Interview QuestionsSQL CommandsMySQL Interview QuestionsVIEW ALL DevOps What is DevOpsDevOps vs AgileDevOps ToolsDevOps TutorialHow To Become A DevOps EngineerDevOps Interview QuestionsWhat Is DockerDocker TutorialDocker Interview QuestionsWhat Is ChefWhat Is KubernetesKubernetes TutorialVIEW ALL Front End Web Development What is JavaScript â€" All You Need To Know About JavaScriptJavaScript TutorialJavaScript Interview QuestionsJavaScript FrameworksAngular TutorialAngular Interview QuestionsWhat is REST API?React TutorialReact vs AngularjQuery TutorialNode TutorialReact Interview QuestionsVIEW ALL Mobile Development Android TutorialAndroid Interview QuestionsAndroid ArchitectureAndroid SQLite DatabaseProgramming aria-current=page>Uncat egorizedEverything You Need To Know Ab... AWS Global Infrastructure C Programming Tutorial: The Basics you Need to Master C Everything You Need To Know About Basic Structure of a C Program How to Compile C Program in Command Prompt? How to Implement Linear Search in C? How to write C Program to find the Roots of a Quadratic Equation? Everything You Need To Know About Sorting Algorithms In C Fibonacci Series In C : A Quick Start To C Programming How To Reverse Number In C? How To Implement Armstrong Number in C? How To Carry Out Swapping of Two Numbers in C? C Program To Find LCM Of Two Numbers Leap Year Program in C Switch Case In C: Everything You Need To Know Everything You Need To Know About Pointers In C How To Implement Selection Sort in C? How To Write A C Program For Deletion And Insertion? How To Implement Heap Sort In C? How To Implement Bubble Sort In C? Binary Search In C: Everything You Need To Know Binary Search Introduction to C P rogramming-Algorithms What is Objective-C: Why Should You Learn It? How To Implement Static Variable In C? How To Implement Queue in C? How To Implement Circular Queue in C? What is Embedded C programming and how is it different? Everything You Need To Know About Sorting Algorithms In C Last updated on May 07,2020 4.2K Views edureka Bookmark Everything You Need To Know About Sorting Algorithms In C This article will will put forth an interesting and an important topic that is Sorting Algorithms In C.Following pointers will be covered in this article,Bubble SortInsertion SortSelection SortQuick SortMerge SortIn simple word, sorting means arranging the given elements or data in an ordered sequence. The main purpose of sorting is to easily quickly locate an element in a sorted list design an efficient algorithm around it. In this blog we will understand different sorting algorithms how to implement them in C.So let us get started then,Bubble SortBubble Sort is a sim ple sorting algorithm which repeatedly compares the adjacent elements of the given array swaps them if they are in wrong order. Suppose we have an array X which contains n elements which needs to be sorted using Bubble Sort. The sorting works as:Pass 1:X[0] X[1] are compared, and swapped if X[0] X[1]X[1] X[2] are compared, and swapped if X[1] X[2]X[2] X[3] are compared, and swapped if X[2] X[3] and so onAt the end of pass 1, the largest element of the list is placed at the highest index of the list.Pass 2:X[0] X[1] are compared, and swapped if X[0] X[1]X[1] X[2] are compared, and swapped if X[1] X[2]X[2] X[3] are compared, and swapped if X[2] X[3] and so onAt the end of Pass 2 the second largest element of the list is placed at the second highest index of the list.Pass n-1:X[0] X[1] are compared, and swapped if X[0] X[1]X[1] X[2] are compared, and swapped if X[1] X[2]X[2] X[3] are compared, and swapped if X[2] X[3] and so onAt the end of this pass. The smallest el ement of the list is placed at the first index of the list.Moving on with this article on Sorting Algorithms In C,Bubble Sort Program in C #include stdio.h // Function to swap elements void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // bubble sort function void bubbleSort(int array[], int n) { int i, j; for (i = 0; i n-1; i++) for (j = 0; j n-i-1; j++) if (array[j] array[j+1]) swap(array[j], array[j+1]); } // Function to print the elements of an array void printArray(int array[], int size) { int i; for (i=0; i size; i++) printf(%d , array[i]); printf(n); } // Main Function int main() { int array[] = {89, 32, 20, 113, -15}; int size = sizeof(array)/sizeof(array[0]); bubbleSort(array, size); printf(Sorted array: n); printArray(array, size); return 0; } Output:Moving on with this article on Sorting Algorithms In C,Insertion SortInsertion Sort is a sorting algorithm where the array is sorted by taking one element at a time. The principle behind insertion sort is to take one element, iterate through the sorted array find its correct position in the sorted array.Step 1 If the element is the first one, it is already sorted. Step 2 Move to next element Step 3 Compare the current element with all elements in the sorted array Step 4 If the element in the sorted array is smaller than the current element, iterate to the next element. Otherwise, shift all the greater element in the array by one position towards right Step 5 Insert the value at the correct position Step 6 Repeat until the complete list is sortedInsertion Sort Program in C #include math.h #include stdio.h // Insertion Sort Function void insertionSort(int array[], int n) { int i, element, j; for (i = 1; i n; i++) { element = array[i]; j = i - 1; while (j = 0 array[j] element) { array[j + 1] = array[j]; j = j - 1; } array[j + 1] = element; } } // Function to print the elements of an array void printArray(int array[], int n) { int i; for (i = 0; i n; i++) printf(%d , array[i]); printf(n); } // Main Function int main() { int array[] = { 122, 17, 93, 3, 56 }; int n = sizeof(array) / sizeof(array[0]); insertionSort(array, n); printArray(array, n); return 0; } OutputMoving on with this article on Sorting Algorithms In C,Selection SortSelection Sort repeatedly searches for the smallest element from the unsorted part of the array and places it at the end of sorted part of the array. Selection sort first finds the smallest element in the unsorted array and swaps it with the first element. Then it finds the second smallest element in the unsorted array and swaps it with the second element, and the algorithm keeps doing this until the entire array is sorted.Selection Sort Program in C #include stdio.h // Function to swap elements void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // Selection Sort void selectionSort(int array[], int n) { int i, j, min_element; for (i = 0; i n-1; i++) { min_element = i; for (j = i+1; j n; j++) if (array[j] array[min_element]) min_element = j; swap(array[min_element], array[i]); } } // Function to print the elements of an array void printArray(int array[], int size) { int i; for (i=0; i size; i++) printf(%d , array[i]); printf(n); } // Main Function int main() { int array[] = {15, 10, 99, 53, 36}; int size = sizeof(array)/sizeof(array[0]); selectionSort(array, size); printf(Sorted array: n); printArray(array, size); return 0; } OutputMoving on with this article on Sorting Algorithms In C,Quick SortQuickSort is a divide conquer algorithm. QuickSort algorithm partitions the complete array around the pivot element. Pivot element can be picked in mulitple ways:First element as pivotLast element as pivotMedian element as pivotRandom element as pivotIn this blog we will be picking the last element as pivot element. partition() is the key process behind the QuickSort algorithm. In partitioning, the pivot element plays an important role. Pivot is placed at its correct position in the sorted array, all the elements smaller than pivot is placed before it, and all the elements greater than pivot is placed after it. All this operation is completed in linear time. Then the array is divided in two parts from the pivot element (i.e. elements less than pivot elements greater than pivot) both the arrays are recursively sorted using Quicksort algorithm.Moving on with this article on Sorting Algorithms In C,Quicksort Progr am in C #includestdio.h // Function to swap two elements void swapElements(int* x, int* y) { int temp = *x; *x = *y; *y = temp; } // Partition function int partition (int arr[], int lowIndex, int highIndex) { int pivotElement = arr[highIndex]; int i = (lowIndex - 1); for (int j = lowIndex; j = highIndex- 1; j++) { if (arr[j] = pivotElement) { i++; swapElements(arr[i], arr[j]); } } swapElements(arr[i + 1], arr[highIndex]); return (i + 1); } // QuickSort Function void quickSort(int arr[], int lowIndex, int highIndex) { if (lowIndex highIndex) { int pivot = partition(arr, lowIndex, highIndex); // Separately sort elements before after partition quickSort(arr, lowIndex, pivot - 1); quickSort(arr, pivot + 1, highIndex); } } // Function to print array void printArray(int arr[], int size) { int i; for (i=0; i size; i++) printf(%d , arr[i]); } // Main Function int main() { int arr[] = {81, 27, 38, 99, 51, 5}; int n = sizeof(arr)/sizeof(arr[0]); quickSort(arr, 0, n-1); printf(Sorted array: ); printArray(arr, n); return 0; } Output:Moving on with this article on Sorting Algorithms In C,Merge SortMerge Sort is one of the best examples of Divide Conquer algorithm. In Merge sort, we divide the array recursively in two halves, until each sub-array contains a single element, and then we merge the sub-array in a way that it results into a sorted array. merge() function merges two sorted sub-arrays into one, wherein it assumes that array[l .. n] and arr[n+1 .. r] are sorted.Merge Sort Program in C #includestdlib.h #includestdio.h // Merge Function void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i n1; i++) L[i] = arr[l + i]; for (j = 0; j n2; j++) R[j] = arr[m + 1+ j]; i = 0; j = 0; k = l; while (i n1 j n2) { if (L[i] = R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i n1) { arr[k] = L[i]; i++; k++; } while (j n2) { arr[k] = R[j]; j++; k++; } } // Merge Sort Function in C void mergeSort(int arr[], int l, int r) { if (l r) { int m = l+(r-l)/2; mergeSort(arr, l, m); mergeSort(arr, m+1, r); merge(arr, l, m, r); } } // Functions to Print Elements of Array void printArray(int A[], int size) { int i; for (i=0; i size; i++) printf(%d , A[i]); printf(n); } // Main Method int main() { int arr[] = {85, 24, 63, 45, 17, 31, 96, 50}; int arr_size = sizeof(arr)/sizeof(arr[0]); printf(Given array is n); printArray(arr, arr_size); mergeSort(arr, 0, arr_size - 1); printf(nSorted array is n); printArray(arr, arr_size); return 0; } Output:Now after going through the above sorting programs you would have understood various sorting algorithms and how to implement them in C language. I hope this blog is informative and added value to you.Now after executing the above program you would have understood the Sorting Algorithms In C. Thus we have come to an end of this article on Quicksort in Java. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edurekas Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate Spring.Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.Recommended blogs for you Big Data Engineer Skills: Skills Required To Become A Big Data Engineer Read Article Top 10 Reasons Why You Should Learn Blockchain Read Article How To Become A DevOps Engineer? | DevOps Engine er Road Map Read Article What is Static Member Function in C++? Read Article RPA Automation Anywhere A Beginners Guide To Automation Anywhere Read Article How to Develop Android App using Kotlin? Read Article What is Decision Table in Software Testing? Read Article Everything You Need To Know About Basic Structure of a C Program Read Article Gossip Protocol in Cassandra Read Article What are the Types of Software Testing Models? Read Article How To Implement Exception Handling In C++? Read Article Cassandra Use Cases Read Article Introduction to NoSQL Database Read Article Why first 100 hours of learning is crucial? Read Article 10 Steps To Create Multiple Virtual Machines Using Vagrant Read Article Vol. XVIII â€" Edureka Career Watch â€" 10th Aug 2019 Read Article How To Best Implement Radix Sort Program In C? Read Article How do you make a Career in Software Testing? Read Article Face Off: MongoDB Vs HBase Vs Cassandra Read Article Appium Tutorial: Know How to Set up Appium Read Article Comments 0 Comments Trending Courses Python Certification Training for Data Scienc ...66k Enrolled LearnersWeekend/WeekdayLive Class Reviews 5 (26200)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.