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

Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort,...
Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort, insertion sort, and merge sort, It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674...
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like...
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to go...
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...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
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...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort,...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort, and selection sort. It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674 8578 8323...
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...
Write a program and test a program that translates the following Bubble Sort algorithm to a...
Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble sort exchanges elements with adjacent elements as it moves the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT