Question

Program this in C There is a lot of sorting algorithms. So far, we have covered...

Program this in C


There is a lot of sorting algorithms. So far, we have covered selection and insertion sort.
However, we only covered how selection sort is implemented using an array. Every sorting
algorithm implemented using an array can also be implemented using a linked list. In this
assignment, you will implement the selection sort algorithm using a linked list instead of an
array.

You will first create an interface for handling string items. Basically, you will need to do the
following:
• Create an interface named item.h
• Define a type Item represents a char * (i.e., a c-string)
• Implement a less function that compares between two strings and return which one
should precede.


Then, you will add to the linked list interface the following functions:
• linkedlistScanInit:
   • The header of the function will be linkedlistScaninit(pLinkedList list).
   • The function takes a linkedlist as input, reads from the command line a set of strings, and store them inside the linkedlist. You can call the function linkedlistAddNode to add a node to the end of the linked list.
• linkedlistShow:
   • The header of the function will be linkedlistShow(pLinkedList list).
   • The function takes a linkedlist as input, loops through the linkedlist and show what inside it

Finally, you will create a main, your main will be as follow:

//Include the needed headers here
int main(void)
{
pLinkedList list = linkedlistInitDefault();
linkedlistScanInit(list);
linkedlistShow(list);
destroy(&list);
return 0;
}

You will use the code in linkedListSt.h and linkedListSt.c

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

`Hey,

Note: In case of any queries, just comment in box I would be very happy to assist all your queries

#include<iostream>

#include<stdlib.h>

using namespace std;

/* This is the List Structure */

typedef struct Node

{

                int data;

                struct Node *link;

}node;

node *head = NULL;//The Head node is used to keep track of linked list

/* Driver functions */

void linkedlistshow();

void swap(node *p1, node*p2);

void SelectionSort(node *head);

void linkedlistscaninit(int data, int position);

/* Main method */

int main()

{

                linkedlistscanit4,1);         //This Insers Element at first position LINKED-LIST = / 4 /

                linkedlistscaninit(2,2);    // Insert Element at second position LINKED-LIST = / 4 2 /

               

                printf("\n Before sorting = ");    

                linkedlistshow();

               

                SelectionSort(head);                      // now we will be sorting linked list

                printf("\n The list After sorting = ");

                linkedlistshow();             

                return 0;

}

/*The function to to sort the linked list */

void SelectionSort(node *head)

{

                node *start = head;

                node *traverse;

                node *min;

               

                while(start->link)

                {

                                min = start;

                                traverse = start->link;

                               

                                while(traverse)

                                {

                                                /*to Find minimum element from array */

                                                if( min->data > traverse->data )

                                                {

                                                                min = traverse;

                                                }

                                               

                                                traverse = traverse->link;

                                }

                                swap(start,min);                                              // Put minimum element on starting location

                                start = start->link;

                }

}

/* swap data field of linked list */

void swap(node *z1, node*z2)

{

                int temp = z1->data;

                z1->data = z2->data;

                z2->data = temp;

}

/* Function for Inserting nodes at defined position */

void linkedlistscanit(int data, int position)

{

                /* firse we will declare node */

                node *temp = (node*)malloc(sizeof(node));

                temp->data = data;

                temp->link = NULL;

                /* if node insertion at first point */

                if(position==1)

                {

                temp->link = head;

                head = temp;

                return ;

                }

               

                /* Adding & Adjusting node links*/

                node *traverse = head;

                for(int i=0; i<position-2; i++)

                {

                traverse = traverse->link;

                }             

                temp->link = traverse->link;

                traverse->link = temp;  

}

/*This is the ffunction for Printing Linked List */

void linkedlistshow()

{

                node *p = head;

               

                while(p)

                {

                                printf(" %d",p->data);

                                p = p->link;

                }

                printf(" \n\n");

}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Program this in C There is a lot of sorting algorithms. So far, we have covered...
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 C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort....

    Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort. Verify the correctness of each implemented algorithm by sorting the following array: 10, 5, 60, 53, 45, 3, 25,37,39,48

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • Sorting: (40 points) a. We studied several sorting algorithms. Every sorting algorithm has their own special...

    Sorting: (40 points) a. We studied several sorting algorithms. Every sorting algorithm has their own special reason where it can only use. Can you explain carefully in which situation the following algorithms would be best sorting algorithm to use in an application. (10 points) i. Insertion sort ii. Selection sort iii. Merge sort iv. Quick sort b. You are given a string of elements as below, Canopus, Mimosa, Betelgeuse, Deneb, Stars, Pollux, Antares, Sirius, Hader i. Your task is to...

  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort...

    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.

  • a. We studied several sorting algorithms. Every sorting algorithm has their own special reason where it...

    a. We studied several sorting algorithms. Every sorting algorithm has their own special reason where it can only use. Can you explain carefully in which situation the following algorithms would be best sorting algorithm to use in an application. (10 points) i. Insertion sort ii. Selection sort iii. Merge sort iv. Quick sort b. You are given a string of elements as below, Canopus, Mimosa, Betelgeuse, Deneb, Stars, Pollux, Antares, Sirius, Hader i. Your task is to insert the above...

  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

    Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...

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
ADVERTISEMENT