This is in data structures using C++
Write and test a version of selection sort for linked lists.
Solution:
#include <bits/stdc++.h>
using namespace std;
// A Linked list node
struct Node {
int data;
struct Node* next;
};
// function to swap nodes 'currX' and 'currY' in a
// linked list without swapping data
void swapNodes(struct Node** head_ref, struct Node* currX,
struct Node* currY, struct Node* prevY)
{
// make 'currY' as new head
*head_ref = currY;
// adjust links
prevY->next = currX;
// Swap next pointers
struct Node* temp = currY->next;
currY->next = currX->next;
currX->next = temp;
}
// function to sort the linked list using
// recursive selection sort technique
struct Node* recurSelectionSort(struct Node* head)
{
// if there is only a single node
if (head->next == NULL)
return head;
// 'min' - pointer to store the node having
// minimum data value
struct Node* min = head;
// 'beforeMin' - pointer to store node previous
// to 'min' node
struct Node* beforeMin = NULL;
struct Node* ptr;
// traverse the list till the last node
for (ptr = head; ptr->next != NULL; ptr = ptr->next) {
// if true, then update 'min' and 'beforeMin'
if (ptr->next->data < min->data) {
min = ptr->next;
beforeMin = ptr;
}
}
// if 'min' and 'head' are not same,
// swap the head node with the 'min' node
if (min != head)
swapNodes(&head, head, min, beforeMin);
// recursively sort the remaining list
head->next = recurSelectionSort(head->next);
return head;
}
// function to sort the given linked list
void sort(struct Node** head_ref)
{
// if list is empty
if ((*head_ref) == NULL)
return;
// sort the list using recursive selection
// sort technique
*head_ref = recurSelectionSort(*head_ref);
}
// function to insert a node at the
// beginning of the linked list
void push(struct Node** head_ref, int new_data)
{
// allocate node
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));
// put in the data
new_node->data = new_data;
// link the old list to the new node
new_node->next = (*head_ref);
// move the head to point to the new node
(*head_ref) = new_node;
}
// function to print the linked list
void printList(struct Node* head)
{
while (head != NULL) {
cout << head->data << " ";
head = head->next;
}
}
// Driver program to test above
int main()
{
struct Node* head = NULL;
// create linked list 10->12->8->4->6
push(&head, 6);
push(&head, 4);
push(&head, 8);
push(&head, 12);
push(&head, 10);
cout << "Linked list before sorting:\n";
printList(head);
// sort the linked list
sort(&head);
cout << "\nLinked list after sorting:\n";
printList(head);
return 0;
}
Output:
Please give thumbsup, if you like it. Thanks.
This is in data structures using C++ Write and test a version of selection sort for...
Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort and Quick sort. For each sort you may store the numbers in an array or a linked list (this may be different for each sort). Write your program so that it accepts arguments from the command line using argc and argv in the main function call.
***PLEASE USE C++ LANGUAGE*** Binary Search of Strings 1. Write a version of the selection sort algorithm presented in the unit, which is used to search a list of strings. 2. Write a version of the binary search algorithm presented in the unit, which is used to search a list of strings. (Use the selection sort that you designed above to sort the list of strings.) 3. Create a test program that primes the list with a set of strings,...
In C++, Write a program to test insertion sort for linked lists
How to write this in c++? 1. Sort the following list using the selection sort algorithm as discussed in this chapter. Show the list after each iteration of the outer for loop. 36, 55, 17, 35, 63, 85, 12, 48, 3, 66 (10 points)
USING C++ write a program that creates a modified version of Merge sort in which insertion sort will be used for merging array elements.
c++, data structure Write a solution to test 2 problem 3 that uses selection sort to sort the items in the vector of integers. #include <iostream> #include <iterator> #include <string> #include <random> using namespace std; void insertionSort(int a[], int N) { int sorted = 0; for (int sorted = 0; sorted < N - 1; ++sorted) { int item_position = sorted + 1; int item = a[item_position]; while (item_position > 0 && a[item_position - 1] > item) { a[item_position] =...
Java - Data Structures Q. Implement one of the sorts ( Selection Sort / Insertion Sort / Shell Sort / Merge Sort ) described in Chapters 8 and 9 . Input is an array with at least 10 items. The items can be of any type (Suggested types are integers—denoting how many patrons visited the library in the last three weeks or strings—denoting the names of books returned today). Print the original data. Print the sorted data.
C++ Programing Consider the following sorting techniques: • Bubble Sort • Insertion Sort • Selection Sort • Merge Sort • Quick Sort Select any two of the above sorting techniques: 1. Write an algorithm (pseudocode) for the two selected sorting techniques 2. Write two separate C++ programs using user defined functions for each of the selected sorting techniques.
1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...
c++ data structures please Pick any inefficient sorting algorithm you want, selection, bubble, insertion, etc., and implement it as a function. Using the system clock as a timer, determine when the efficiency of the algorithm breaks down. For example, does it become slow after 1000 elements, 10000 elements, 100000 elements? Write some code to figure this out. Then use the sort() algorithm that is part of the STL <algorithm> library. How does this function compare to the function you implemented?...