Question

c++ code for shell sort implementing singly-linked list. rearranging Node to sort.

c++ code for shell sort implementing singly-linked list. rearranging Node to sort.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;
int ssort(int a[], int n)
{
// begins with a large gap (g), then reduce the gap.
for (int g = n/2; g > 0; g /= 2)
{
// gap sorted
for (int i = g; i < n; i += 1)
{
// add a[i] to the elements that have been gap sorted & save a[i] in temp and make a hole at position i
int temp = a[i];
// Push the earlier gap-sorted items up to the right one
int j;   
for (j = i; j >= g && a[j - g] > temp; j -= g)
a[j] = a[j - g];
a[j] = temp;
}
}
return 0;
}
  
int main() //main function
{
int a[] = {12, 34, 54, 2, 3}, i;
int n = sizeof(a)/sizeof(a[0]);
cout << "Input: \n";
for (int i=0; i<n; i++)
cout << a[i] << "-";
ssort(a, n);
cout << "\nOutput: \n";
for (int i=0; i<n; i++)
cout << a[i] << "-";
return 0;
}

Thanks, please give a thumbs up.

Add a comment
Know the answer?
Add Answer to:
c++ code for shell sort implementing singly-linked list. rearranging Node to sort.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java, give a complete  example of implementing the NODE of singly linked list. (Screenshot) Thanks.

    In Java, give a complete  example of implementing the NODE of singly linked list. (Screenshot) Thanks.

  • Revise the Shell sort so that it could process SLL Note: process linked list Node rather...

    Revise the Shell sort so that it could process SLL Note: process linked list Node rather than just processing an array of data of the Single link list Gap: h/2 for Shell Sort Any help would be appreciated

  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

  • You are given a pointer to a singly linked list. The singly linked list has cycles...

    You are given a pointer to a singly linked list. The singly linked list has cycles due to a programming error. Write a C program to detect whether the linked list has cycles without storing all the pointers to the nodes. The function detect_cycles should return 1 when a cycle is found and 0 when there are no cycles. Your code should handle all corner cases carefully and should not cause segmentation fault. Figure shows various examples of cycles for...

  • Write a C# function bool HasCycle(Node head) that detects a cycle in a singly linked list....

    Write a C# function bool HasCycle(Node head) that detects a cycle in a singly linked list. It must return a boolean true if the list contains a cycle, or false otherwise. HasCycle has a reference to a Node object, that points to the head of a linked list, as parameter.

  • Answer all questions 1- in circular singly linked list, previous pointer of the first node points...

    Answer all questions 1- in circular singly linked list, previous pointer of the first node points to which node A. First node B. Itself C. null D. Last node 2- Which of the following is NOT an applications of linked lists? A. Implementation of stacks and queues B. Dynamic memory allocation C. Manipulation of polynomials D. Keeping people at home during epidemics like corona virus 3- In a circular singly linked list? A. Components are all linked together in some...

  • Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using...

    Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using only a constant amount of additional storage and no recursion. Assume the list object has a head pointer _head and consists of Node objects; a Node object has a pointer Node* _next

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • What is the difference between a doubly and singly linked list and what would this code...

    What is the difference between a doubly and singly linked list and what would this code look like if it were a doubly linked list? template <typename Object> class List { private:        struct Node     {         Object data;         Node   *prev;         Node   *next;         Node( const Object & d = Object{ }, Node * p = nullptr, Node * n = nullptr )           : data{ d }, prev{ p }, next{ n } { }        ...

  • I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas....

    I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas. public class Node ! int id; Node next: public Node (int id) ( this.id id: Write program codes for the traditional: 1) append int id), prepend(int id), removeFirstNodeO. displayAlINodesO, and findById(int id) operations for a singly linked list 2) pushint id), pop), peek0, displayAllNodes0 operations for a stack 3) enQueue(int id), deQueuel), displayAINodes() operations for a gueue Please make sure that you declare separate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT