Question

In: Computer Science

1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from...

1.   Bubble Sort
Implement a bubble sort program that will read from a file “pp2.txt” from the current directory a list of intergers (10 numbers to be exact), and the sort them, and print them to the screen. You can use redirection to read data from a given file through standard input, as opposed to reading the data from the file with the read API (similar to Lab #1). You can assume the input data will only have 10 numbers in the file, and that the numbers will be valid integers. You must implement 3 functions, including main(), bubblesort(), and swap(). The function declarations are included, and they must be followed exactly to receive full points. All your code should be contained in the bubblesort.c file, and the Makefile.

Usage:./sort
For example, if you type:
./sort
INPUT: 1 3 2 8 4 9 8 3 10 14
OUTPUT: 1 2 3 3 4 8 8 9 10 14

Bubblesort.c:
#include <stdio.h>
void bubblesort(int [], int);
void swap(int*,int*);

int main()
{
   printf("edit the bubblesort.c file to implement your bubble sort\n");
   return 0;
}

The file that should be read:

pp2.txt:

1 3 2 8 4 9 8 3 10 14

Solutions

Expert Solution

CODE:

#include<stdio.h>
#include<stdlib.h>

//function declaration bubblesort and swap
void bubblesort(int [], int);
void swap(int*,int*);

//main function
int main(){
  
//   declare a file pointer to open file
   FILE *file;
  
   int element;
  
//   declare a array of size 10
   int arr[10];
   int i = 0;
  
//   declare index to hold the index of array
   int index=0;
  
//   declare size of array n = 10
   int n = 10;
  
//   open the file in read mode
   file = fopen("pp2.txt","r");
  
//   check if the file exists or not
// if file does not exists then exit the program
   if(file==NULL){
      
       printf("Error! File does not exists\n");
      
       exit(1);
   }
  
//read a line from the file
while (fscanf(file, " %d", &element) == 1) {
  
//   add the element to the array
//   increment the index value by 1 to store next element
   arr[index++] = element;
}

//make a call to bubblesort pass array and size as parameters
bubblesort(arr,n);

//use the for loop to print the array elements after sorting
for( i =0;i<10;i++){
  
//   print the elements in array
   printf("%d ",arr[i]);
}  

   return 0;
}

//bubble sort function that takes two parameters array and size of array n
//return the array after sorting
void bubblesort(int arr[],int n){
  
//   declare two variables i and j
   int i,j;
  
//   use nested for loops to traverse through the array
   for( i = 0;i<n-1;i++){
       for( j=0;j<n-i-1;j++){
          
//           check if the previous element is greater than next element
//           if true then make a call to swap
           if(arr[j]>arr[j+1]){
              
//               make a call to swap to swap elements
               swap(&arr[j],&arr[j+1]);
           }
       }
   }
}

//function that takes two reference parameters
//return the two values after swapping
void swap(int* a,int* b){
  
   //declare the temp variable
   int temp;
  
//   store value of a in temp
   temp = *a;
  
//   update a value with b
   *a = *b;
  
//   update b value with temp
   *b = temp;
}



Related Solutions

Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using BUBBLE SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_BubbleSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does...
(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. Integers are separated by spaces in the file. Read data back from the file and display the data in increasing order. After writing the file to disk, the input file should be read into an array, sorted using the static Arrays.sort() method from the Java API and then displayed in the...
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into ascending order. Use the Bubble Sort algorithm from the lecture. You can use either Base Addressing or Indexed Addressing for the arrays. For this assignment, make sure you prompt the user for the numbers. Do not hard-code them in the data section. NOTE: Declare the array last in the Data section.
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
C++ Question: we need to read speech from .txt file. Steve Jobs delivered a touching and...
C++ Question: we need to read speech from .txt file. Steve Jobs delivered a touching and inspiring speech at Stanford's 2005 commencement. The transcript of this speech is attached at the end of this homework description. In this homework, you are going to write a program to find out all the unique tokens (or words) used in this speech and their corresponding frequencies, where the frequency of a word w is the total number of times that w appears in...
Bubble and Selection Sort For this assignment, you are to consider bubble and selection sort. Both...
Bubble and Selection Sort For this assignment, you are to consider bubble and selection sort. Both are O(n^2) however it may be possible to classify one algorithm as being more efficient than the other. You are to discuss which algorithm you feel is the most efficient and in what cases it will be more efficient. Provide any relevant test cases and code to support your belief. Submit a pdf containing your findings and test results along with any relevant code...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT